2011-01-28 96 views
8

我已經能夠覆蓋任何名稱都帶有「android:」前綴的主題,但Android themes.xml也定義了似乎不能被覆蓋的屬性。例如:覆蓋默認的Android主題

<!-- Variation on the Light theme that turns off the title --> 
<style name="Theme.Codebase" parent="android:style/Theme.Light"> 
    <item name="android:windowNoTitle">true</item> 
    <item name="android:windowContentOverlay">@null</item> 
    <item name="colorBackground">@color/off_white</item> 
</style> 

colorBackground在Theme.Light XML定義,但在這裏添加這給了我一個

/res/values/styles.xml:10: error: Error: No resource found that matches the given name: attr 'colorBackground'. 

錯誤。如何覆蓋應用程序的整體風格?

+0

那些沒有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

回答

8

您可以覆蓋標準屬性已修改性能如windowNoTitle以同樣的方式,只是不要忘了添加android:前綴是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="SEclubTheme" parent="@android:style/Theme"> 
     <item name="android:colorForeground">@color/bright_foreground_dark</item> 
     <item name="android:colorBackground">@color/background_dark</item> 
    </style> 
</resources> 
+0

是的,我明白了。對於「colorBackground」如何在其主題中獨立運作,我仍然感到困惑。任何想法?現在最好的答案! – typeoneerror 2011-02-08 17:14:58

3

沒有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> 

在這個例子中,因爲我用的自定義主題名稱MyDarkThemeMyLightTheme,他們需要在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()是在運行時選擇主題的一種方式。另一種方法是在valuesvalues-11,values-14的資源下定義styles.xml的多個版本,該默認主題,Android 3.0主題(API-11)和Android 4.0主題(API-14)對應的資源。