【问题标题】:How to add a background color for an Android app through Theme?如何通过 Theme 为 Android 应用添加背景颜色?
【发布时间】:2017-06-24 05:21:11
【问题描述】:

我创建了一个主题并通过 AndroidManifest.xml 应用了它

<style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">

    <item name="colorPrimary">#8BC34A</item>
    <item name="colorPrimaryDark">#689F38</item>
    <item name="colorAccent">#8BC34A</item>
    </style>

这个很好用

但是当我添加颜色选项时:背景 我收到一个错误:

错误:(1468, 21) 找不到与给定名称匹配的资源:attr 'colorBackground'。

当我双击错误时,它会指向 Values.xml

如何通过主题添加背景色?

【问题讨论】:

    标签: android material-design android-theme


    【解决方案1】:

    尝试使用下面的代码...

    先定义颜色:

    <color name="background">#FF0000 </color>
    

    创建引用该颜色的样式/主题:

     <style name="MyTheme" parent="@android:style/Theme.Light"> 
      <item name="android:windowBackground">@color/background</item>
     </style>
    

    在您的 AndroidManifest.xml 中的活动中应用此主题。

     <activity
            android:name=".MyActivity"
            android:theme="@style/MyTheme" />
    

    【讨论】:

      【解决方案2】:

      以您的MyTheme 风格试试这个。

      <item name="android:colorBackground">@color/colorBackground</item>
      

      Android Studio 内置了一个主题编辑器,当 style.xml 打开时,你会在右上角看到打开编辑器文本点击它。使自定义主题变得非常容易。

      【讨论】:

        猜你喜欢
        • 2019-02-28
        • 1970-01-01
        • 2020-06-10
        • 1970-01-01
        • 1970-01-01
        • 2016-08-29
        • 2012-11-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多