背景drawable不起作用

时间:2017-05-31 19:38:16

标签: android

我已创建此可绘制资源backgroundactivity,以用作我应用的背景:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <item
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <shape
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <gradient
                android:angle="90"
                android:startColor="#BFEFFF"
                android:centerColor="#B0E2FF"
                android:endColor="#82CFFD"
                android:type="linear"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </shape>
    </item>
</selector>

我想在我的所有活动中设置此背景,因此我尝试将此可绘制资源放在styles.xml中,如下所示:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <item name="android:background">@drawable/backgroundactivity</item>
    </style>

    <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="@android:background">@color/colorPrimary</item>
    </style>
</resources>

但是以这种方式它并没有起作用:背景仍然是默认的白色。我还尝试将android:background替换为android:windowBackground,但这会导致我的应用程序完全变黑。

提前致谢!

1 个答案:

答案 0 :(得分:1)

假设您的backgroundactivity.xml已正确完成,请删除.xml扩展名。

<item name="android:background">@drawable/backgroundactivity.xml</item>

<item name="android:background">@drawable/backgroundactivity</item>
相关问题