我看到過許多關於此錯誤的SO問題,但他們都有一個共同的問題,那就是他們試圖在舊版本上使用v21屬性或者他們有較低的targetSDKVersion
,但在我的應用中並不是這種情況。找不到與指定名稱相匹配的資源:attr'accentColor'
注:我刪除了已經存在的文件夾values-v11
和values-v14
從項目,如果該事項。
的編譯器錯誤如下:
[2015-08-08 14:30:23 - MaterialDesignGoogleNowLikeSearchBoxThree] D:\CodingMyHeart\WORKSPACES\EclipseWorkspaces\Workspace of Android Practice One\MaterialDesignGoogleNowLikeSearchBoxThree\res\values\styles.xml:19: error: Error: No resource found that matches the given name: attr 'accentColor'.
[2015-08-08 14:30:23 - MaterialDesignGoogleNowLikeSearchBoxThree]
[2015-08-08 14:30:23 - MaterialDesignGoogleNowLikeSearchBoxThree] D:\CodingMyHeart\WORKSPACES\EclipseWorkspaces\Workspace of Android Practice One\MaterialDesignGoogleNowLikeSearchBoxThree\res\values\styles.xml:17: error: Error: No resource found that matches the given name: attr 'primaryColor'.
[2015-08-08 14:30:23 - MaterialDesignGoogleNowLikeSearchBoxThree]
[2015-08-08 14:30:23 - MaterialDesignGoogleNowLikeSearchBoxThree] D:\CodingMyHeart\WORKSPACES\EclipseWorkspaces\Workspace of Android Practice One\MaterialDesignGoogleNowLikeSearchBoxThree\res\values\styles.xml:18: error: Error: No resource found that matches the given name: attr 'primaryColorDark'.
[2015-08-08 14:30:23 - MaterialDesignGoogleNowLikeSearchBoxThree]
[2015-08-08 14:30:23 - MaterialDesignGoogleNowLikeSearchBoxThree] D:\CodingMyHeart\WORKSPACES\EclipseWorkspaces\Workspace of Android Practice One\MaterialDesignGoogleNowLikeSearchBoxThree\res\values-v21\styles.xml:7: error: Error: No resource found that matches the given name: attr 'android:accentColor'.
[2015-08-08 14:30:23 - MaterialDesignGoogleNowLikeSearchBoxThree]
[2015-08-08 14:30:23 - MaterialDesignGoogleNowLikeSearchBoxThree] D:\CodingMyHeart\WORKSPACES\EclipseWorkspaces\Workspace of Android Practice One\MaterialDesignGoogleNowLikeSearchBoxThree\res\values-v21\styles.xml:5: error: Error: No resource found that matches the given name: attr 'android:primaryColor'.
[2015-08-08 14:30:23 - MaterialDesignGoogleNowLikeSearchBoxThree]
[2015-08-08 14:30:23 - MaterialDesignGoogleNowLikeSearchBoxThree] D:\CodingMyHeart\WORKSPACES\EclipseWorkspaces\Workspace of Android Practice One\MaterialDesignGoogleNowLikeSearchBoxThree\res\values-v21\styles.xml:6: error: Error: No resource found that matches the given name: attr 'android:primaryColorDark'.
[2015-08-08 14:30:23 - MaterialDesignGoogleNowLikeSearchBoxThree]
我已經包含在項目appcompat_v7
,並且有一個名爲android-support-v7-appcompat
另一個庫可供選擇:
SSCCE:
RES /值/ colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="primaryColor">#64FFDA</color><!-- Light ferozi -->
<color name="primaryColorDark">#1DE9B6</color><!-- Darker ferozi -->
<color name="accentColor">#E94F37</color><!-- Close to red -->
</resources>
RES /值/ styles.xml
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="primaryColor">@color/primaryColor</item>
<item name="primaryColorDark">@color/primaryColorDark</item>
<item name="accentColor">@color/accentColor</item>
</style>
</resources>
RES /值-V21/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:primaryColor">@color/primaryColor</item>
<item name="android:primaryColorDark">@color/primaryColorDark</item>
<item name="android:accentColor">@color/accentColor</item>
</style>
</resources>
清單
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.materialdesigngooglenowlikesearchboxthree"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.materialdesigngooglenowlikesearchboxthree.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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);
}
}
嘿,這已經解決了錯誤。但是這些顏色沒有被應用。它只是根據我是使用基本AppCompat.Light還是AppCompat.Dark這個主題來選擇亮或暗的actionbar顏色。我該怎麼辦? – Solace
你使用默認的'actionBar'或'toolbar'嗎? – ImMathan
默認的ActionBar。我沒有添加任何代碼。你可以看到上面的SSCCE。 – Solace