【问题标题】:Why doesn't android:colorBackground work?为什么 android:colorBackground 不起作用?
【发布时间】:2016-03-31 21:03:05
【问题描述】:

在这个 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>

style.xml

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

结果如下所示(没有红色背景):

【问题讨论】:

  • 在您的 xml 中使用背景并添加此颜色。
  • android:background 也将颜色应用到 ActionBar 。
  • 我通过answer解决了这个问题
  • 通过answer解决了问题

标签: android


【解决方案1】:

您可以在您的 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>

【讨论】:

  • 它有效。我正在阅读 Android Programming: The Big Nerd Ranch Guide ,它给出了 android:colorBackground 。我很困惑为什么android:colorBackground 不起作用。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 2013-03-07
    • 2019-08-06
    • 2016-07-05
    • 2011-08-06
    • 2013-01-18
    相关资源
    最近更新 更多