2011-01-28 114 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)对应的资源。