How to make custom windowBackground theme style wo

2020-03-19 01:49发布

I am trying to get all of my activities to have a custom theme style that should look like this:

good

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:

bad

I have the following in my AndroidManifest.xml's application tag:

android:theme="@style/AppTheme"

The theme is as follows in 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>

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>

Finally, this appears in styles.xml under res/values-v14:

<style name="AppBaseTheme" parent="@android:style/Theme.DeviceDefault.Light">
</style>

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?

EDIT: 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.

2条回答
Lonely孤独者°
2楼-- · 2020-03-19 02:28

I found that the latest version of Android Studio expect that the parent for the style needs to be Theme.AppCompat. Also, it is good style to create a colors.xml file in the values directory (with named color elements between the resource tags). Put your RGB values as values to the named color elements. Reference them in your styles with @color/.

<!--colors.xml in values folder-->
<resources>
    <color name="color1">#ffb62a23</color>
    <color name="color2">#ffedeba6</color>
    <color name="color3">#00ff00</color>
</resources>

<!--style tags -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowBackground">@color/color2</item>
    <item name="android:textColorPrimary">@color/color1</item>
    <item name="android:textColorSecondary">@color/color2</item>
    <item name="android:textColorTertiary">@color/color3</item>
</style>
查看更多
够拽才男人
3楼-- · 2020-03-19 02:48

The key to solving the problem was changing android:windowBackground in the theme to the following:

<item name="android:windowBackground">@drawable/default_background</item>

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.

查看更多
登录 后发表回答