沒有ATTR前綴,你colorBackground成爲一個屬性,你需要定義。請看下面的例子,其中theme_dependent_icon
是在styles.xml
定義:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<declare-styleable name="custom_menu">
<attr name="theme_dependent_icon" format="reference"/>
</declare-styleable>
<style name="MyDarkTheme" parent="android:Theme" >
<item name="theme_dependent_icon">@drawable/ic_search_dark</item>
</style>
<style name="MyLightTheme" parent="android:Theme.Light" >
<item name="theme_dependent_icon">@drawable/ic_search_light</item>
</style>
</resources>
然後,你可以在你main_activity.xml
通過?attr/theme_dependent_icon
使用屬性:
<LinearLayout 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:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="?attr/theme_dependent_icon" />
</LinearLayout>
在這個例子中,因爲我用的自定義主題名稱MyDarkTheme
和MyLightTheme
,他們需要在setContentView
之前的主要活動onCreate
期間選擇,即
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.MyDarkTheme); // causes ic_search_dark.png to be shown
// setTheme(R.style.MyLightTheme); // causes ic_search_light.png to be shown
setContentView(R.layout.main_activity);
}
調用setTheme()是在運行時選擇主題的一種方式。另一種方法是在values
,values-11
,values-14
的資源下定義styles.xml
的多個版本,該默認主題,Android 3.0主題(API-11)和Android 4.0主題(API-14)對應的資源。
那些沒有android的標籤似乎是在android源代碼中的相同res/values文件夾中的attr xml文件中定義的。 http://android.git.kernel.org/?p=platform/frameworks/base.git;a=tree;f=core/res/res/values;hb=HEAD看看attrs.xml和attrs_manifest.xml我認爲你需要使用xmlns以某種方式導入這些文件,或者將相似的文件添加到你的值文件夾中,但我對xml的瞭解不夠。 – Jems 2011-01-28 23:30:44