Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 覆盖默认的Android主题_Java_Android - Fatal编程技术网

Java 覆盖默认的Android主题

Java 覆盖默认的Android主题,java,android,Java,Android,我已经能够覆盖任何名称前面带有“android:”的主题,但是android themes.xml还定义了似乎无法覆盖的属性。例如: <!-- Variation on the Light theme that turns off the title --> <style name="Theme.Codebase" parent="android:style/Theme.Light"> <item name="android:windowNoTitle"&g

我已经能够覆盖任何名称前面带有“android:”的主题,但是android themes.xml还定义了似乎无法覆盖的属性。例如:

<!-- 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>

错误。如何覆盖整个应用程序的样式?

您可以用修改属性(如
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>

@颜色/明亮\前景\黑暗
@颜色/背景颜色为深色

如果没有attr前缀,您的colorBackground将成为需要定义的属性。考虑下面的例子,在代码< >样式> .xml < /代码>:

中定义<代码>主题依赖图标I/COD>
<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>
<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()是在运行时选择主题的一种方法。另一种方法是在资源的
值-11
值-14
下定义多个版本的
style.xml
,对应默认主题、Android 3.0主题(API-11)和Android 4.0主题(API-14).

那些没有android的标签:似乎是在android源代码中相同res/values文件夹中的attr xml文件中定义的。看看attrs.xml和attrs_manifest.xml,我想你要么需要使用xmlns导入它们,要么将类似的文件添加到你的values文件夹中,但我对xml的了解还不够确定。是的,我知道了。我仍然对“colorBackground”在他们的主题中是如何独立工作的感到困惑。有什么想法吗?现在最好的答案!我可以这么说。它定义了
colorBackgroundCacheHint
参数,该参数用于
ListView
样式中,根据视图始终绘制在纯色背景上的假设执行一些优化。此外,该属性似乎没有任何用处。更重要的是
windowBackground
属性,顾名思义,它定义了窗口背景。
@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);
}