为什么android:colorBackground不起作用?

时间:2015-12-26 07:21:58

标签: android

只是在HelloWorld应用程序中,我使用android:colorBackground属性,但它不适用红色背景。 的AndroidManifest.xml

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".EditTextActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

布局xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".EditTextActivity">

    <TextView
        android:text="Hello World!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40sp"
        />
</RelativeLayout>

样式xml:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:colorBackground">#ff0000</item>
    </style>
</resources>

结果如(没有红色背景): Hello World

2 个答案:

答案 0 :(得分:3)

您可以在RelativeLayout中使用android:background="#FF0000"或为v21创建styles.xml并写下:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowBackground">@color/redcolor</item>

</style>

也在你的styles.xml中:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowBackground">@color/redcolor</item>
</style>

答案 1 :(得分:0)

我不知道为什么会发生您的问题,但是我所做的只是将十六进制代码作为自定义颜色放入colors.xml中,然后在android:colorBackground中引用它。这样,背景颜色才真正适用。

相关问题