[英]Override default Android themes
I've been able to override any themes that have names with "android:" prepended to them, but the Android themes.xml also defines properties that don't seem to be able to be overridden. 我已经能够覆盖任何名称前缀为“android:”的主题,但Android themes.xml也定义了似乎无法覆盖的属性。 For example: 例如:
<!-- 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 is defined in the Theme.Light xml, but adding this here gives me a colorBackground在Theme.Light xml中定义,但是在这里添加它给了我一个
/res/values/styles.xml:10: error: Error: No resource found that matches the given name: attr 'colorBackground'.
error. 错误。 How do I override that style for the Application as a whole? 如何覆盖整个应用程序的样式?
You can overwrite standard attributes the same way you modified such properties as windowNoTitle
, just don't forget to add android:
prefix like this: 您可以像修改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>
Without the attr prefix, your colorBackground becomes an attribute that you need to define. 如果没有attr前缀,colorBackground将成为您需要定义的属性。 Consider the following example where theme_dependent_icon
is defined in a styles.xml
: 请考虑以下示例,其中在styles.xml
定义theme_dependent_icon
:
<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>
Then, you can use the attribute via ?attr/theme_dependent_icon
in your main_activity.xml
: 然后,您可以在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>
In this example, because I used custom theme names MyDarkTheme
and MyLightTheme
, they need to be selected during onCreate
of your main activity prior to setContentView
, ie 在此示例中,因为我使用自定义主题名称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);
}
Calling setTheme() is one way of selecting a theme during runtime. 调用setTheme()是在运行时选择主题的一种方法。 Another way is to define multiple versions of styles.xml
in your resources under the values
, values-11
, values-14
corresponding to default theme, theme for Android 3.0 (API-11) and theme for Android 4.0 (API-14). 另一种方法是定义的多个版本styles.xml
在你的资源下的values
, values-11
, values-14
对应的预设主题,为Android 3.0(API-11)主题和主题为Android 4.0(API-14)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.