【问题标题】:Android find default background colorAndroid查找默认背景颜色
【发布时间】:2016-12-06 16:28:12
【问题描述】:

我有一个带有简单回收视图的应用。

布局声明如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/contentView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/some_id" />

</RelativeLayout>

我注意到在不同的设备上,recyclerView 实际上有不同的背景。在较新的设备中,背景只是白色。在旧设备上,背景为浅灰色。

在 android studio 中,设计视图中的背景颜色为空白。

所以我的问题是,这种灰色是从哪里来的?我怎样才能将其普遍更改为白色?

我显然可以在这个特定的视图中添加背景:白色。但是有没有办法覆盖系统默认值?

【问题讨论】:

  • 除非你有大量的 RecyclerViews,否则这完全是矫枉过正。这可能与在整个系统版本中更改的默认系统设置有关。只需设置背景颜色即可。
  • 对我来说是#f6f6f6,我通过反复试验实现了它。 :-)

标签: android


【解决方案1】:

Android 默认背景颜色

 <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="?android:colorBackground">
 </RelativeLayout

使用

 android:background="?android:colorBackground"

【讨论】:

    【解决方案2】:

    你看到的其实是activity的背景,而不是recyclerview,默认是透明的。颜色确实改变了每个 Android 版本的查看时间。

    您可以在您的应用主题中覆盖它。

    首先在 values/colors.xml 中定义颜色

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

    在引用该颜色的 res/values 中创建一个主题.xml 文件:

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

    然后在您的 AndroidManifest.xml 中将其指定为 您的活动要使用的主题。

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

    https://stackoverflow.com/a/10157077/4623782

    【讨论】:

      【解决方案3】:

      任何android studio项目中默认设置的背景颜色为#fafafa

      【讨论】:

        【解决方案4】:

        因为主题。在 res/values/style.xml 中指定您的主题。或在视图定义中手动设置。

        <android.support.v7.widget.RecyclerView
            android:id="@+id/contentView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/some_id"
            android:background="@android:color/white"
         />
        

        它将在所有设备上为您提供白色背景

        【讨论】:

        • 直接在布局上设置背景会导致不必要的过度绘制。最好在主题样式上设置背景,如接受的答案所示。
        猜你喜欢
        • 2013-11-19
        • 1970-01-01
        • 2011-10-09
        • 1970-01-01
        • 1970-01-01
        • 2013-06-26
        • 2021-11-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多