从2天开始,我尝试在新应用中使用材料设计(sdk 20 - )。
我阅读了一些教程并使用了这个[http://android-developers.blogspot.fr/2014/10/appcompat-v21-material-design-for-pre.html][1]我发生了这个错误:
C:\用户\马克西姆\桌面\ MaterialApp \应用\ SRC \主\ RES \值\的themes.xml 错误:检索项目的父项时出错:找不到与给定名称“android:Theme.AppCompat.Light”匹配的资源。 错误:任务':app:processDebugResources'的执行失败。 com.android.ide.common.internal.LoggedErrorException:无法运行命令: C:\ Users \ Maxime \ AppData \ Local \ Android \ android-studio3 \ sdk \ build-tools \ 21.0.2 \ aapt.exe package -f --no-crunch -IC:\ Users \ Maxime \ AppData \ Local \ Android \ android-studio3 \ sdk \ platforms \ android-21 \ android.jar -MC:\ Users \ Maxime \ Desktop \ MaterialApp \ app \ build \ intermediates \ manifests \ debug \ AndroidManifest.xml -SC:\ Users \ Maxime \ Desktop \ MaterialApp \ app \ build \ intermediates \ res \ debug -AC:\ Users \ Maxime \ Desktop \ MaterialApp \ app \ build \ intermediates \ assets \ debug -m -JC:\ Users \ Maxime \ Desktop \ MaterialApp \ app \ build \ generated \ source \ r \ _ debug:FC:\ Users \ Maxime \ Desktop \ MaterialApp \ app \ build \ intermediates \ libs \ app-debug.ap_ --debug-mode --custom-package com.maxime.myapplication - 0 apk --output-text-symbols C:\ Users \ Maxime \ Desktop \ MaterialApp \ app \ build \ intermediates \ symbols \ debug 错误代码: 1 输出: C:\ Users \ Maxime \ Desktop \ MaterialApp \ app \ build \ intermediates \ res \ debug \ values \ values.xml:2411:错误:检索项目的父项时出错:找不到与给定名称'android:Theme匹配的资源。 AppCompat.Light”。
我添加了我创建的文件以获取此信息:
/res/values/themes.xml
<resources>
<style name="Theme.MyTheme" parent="android:Theme.AppCompat.Light">
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">@color/yellow</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">@color/blue</item>
<!-- colorAccent is used as the default value for colorControlActivated,
which is used to tint widgets -->
<item name="colorAccent">@color/green</item>
<!-- You can also set colorControlNormal, colorControlActivated
colorControlHighlight, and colorSwitchThumbNormal. -->
</style>
/res/layout/activity_home.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.maxime.materialapp.HomeActivity">
<android.support.v7.widget.Toolbar
layout_height="match_parent"
layout_width="match_parent"
android:id="@+id/toolbar"
minHeight="?attr/actionBarSize"
background="?attr/colorPrimary" />
/java/package/HomeActivity.java
public class HomeActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
最后,无法识别setSupportActionBar(工具栏)。
我的gradle:`apply plugin:'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.0.2"
defaultConfig {
applicationId "com.maxime.myapplication"
minSdkVersion 19
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile "com.android.support:appcompat-v7:21.0.+"
}
答案 0 :(得分:1)
AppCompat主题为Theme.AppCompat.Light
,而不是android:Theme.AppCompat.Light
答案 1 :(得分:0)
尝试将styles.xml更改为此<style name="AppTheme" parent="android:Theme.Material">
并参阅本教程
https://developer.android.com/training/material/index.html
答案 2 :(得分:0)
如果您要使用工具栏的支持版本(ActionBarActivity
),则需要将setSupportActionBar
与Activity
一起使用,而不是android.support.v7.widget.Toolbar
。< / p>
API级别21中添加了
Activity.setActionBar(android.widget.Toolbar toolbar)
,因此无法与minSdkVersion 19
此外,如Ian Lake所述,您必须使用Theme.AppCompat.Light
,而不是android:Theme.AppCompat.Light