为什么 android:colorBackground 不起作用?

sha*_*ihe 1 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>
Run Code Online (Sandbox Code Playgroud)

布局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>
Run Code Online (Sandbox Code Playgroud)

样式.xml :

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:colorBackground">#ff0000</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

结果如下(没有红色背景): 你好,世界

Mah*_*uri 5

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

<resources>

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

</style>
Run Code Online (Sandbox Code Playgroud)

也在你的 styles.xml 中:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowBackground">@color/redcolor</item>
</style>
Run Code Online (Sandbox Code Playgroud)