如何使自定义windowBackground主题样式在所有Android设备上都能工作? [英] How to make custom windowBackground theme style work on all Android devices?
问题描述
我正在尝试使我的所有活动都具有自定义主题样式,该样式应如下所示:
I am trying to get all of my activities to have a custom theme style that should look like this:
此主题可在许多设备上使用,包括Nexus 7,三星Galaxy S4和三星Droid Charge(运行姜饼).但是,在其他设备(例如HP Slate 7和Motorola Droid RAZR)上,它最终看起来像这样:
This theme works on many devices, including the Nexus 7, the Samsung Galaxy S4, and the Samsung Droid Charge (running Gingerbread). However, on other devices, such as HP Slate 7 and Motorola Droid RAZR, it ends up looking like this:
我在AndroidManifest.xml
的application
标记中包含以下内容:
I have the following in my AndroidManifest.xml
's application
tag:
android:theme="@style/AppTheme"
主题在res/values/styles.xml
中如下:
<style name="AppBaseTheme" parent="@android:style/Theme.Light.NoTitleBar">
</style>
<style name="AppTheme" parent="@style/AppBaseTheme">
<item name="android:windowBackground">@color/background</item>
</style>
我还在res/values-v11
下添加了styles.xml
.这是适用的样式:
I have also added a styles.xml
under res/values-v11
. Here is the applicable style from there:
<style name="AppBaseTheme" parent="@android:style/Theme.Holo.Light.NoActionBar">
</style>
最后,它出现在res/values-v14
下的styles.xml
中:
Finally, this appears in styles.xml
under res/values-v14
:
<style name="AppBaseTheme" parent="@android:style/Theme.DeviceDefault.Light">
</style>
我尝试为每个活动定义主题,并在onCreate()
中手动使用setTheme()
功能,但是没有任何效果.我也尝试过在每个Activity
中手动设置背景,但这也不起作用.我该怎么做才能解决我的问题?
I have tried to define the theme for each activity and to manually use the setTheme()
function in onCreate()
, but nothing has worked. I have also tried to manually set a background in every Activity
, and this did not work either. What can I do to fix my problem?
有趣的是在样式中设置android:background
使其起作用,但随后不应该具有背景的元素也将获得该背景颜色.
What's interesting is setting android:background
in the styles makes it work, but then elements that should not have backgrounds receive that background color as well.
推荐答案
解决问题的关键是将主题中的android:windowBackground
更改为以下内容:
The key to solving the problem was changing android:windowBackground
in the theme to the following:
<item name="android:windowBackground">@drawable/default_background</item>
请注意,我不再使用@color
,而只是使用@drawable
,其xml如下:
Notice that I am no longer using @color
, but simply a @drawable
, the xml for which is below:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="@color/background"/>
</shape>
似乎某些设备不支持接受该元素的颜色.
It seems that some devices do not support accepting a color for this element.
这篇关于如何使自定义windowBackground主题样式在所有Android设备上都能工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!