How to setup night mode drawables to work as expected

不羁的心 提交于 2019-12-10 21:39:34

问题


I what to change background as night mode changes.

I have /values and /values-night folder, that contain "colors.xml" with different values. `

<color name="grey1">#ebebeb</color>
<color name="grey2">#c7c7c7</color>
<color name="grey3">#999999</color>
<color name="hover1">#8bb065</color>
<color name="red1">#ba0000</color>
<color name="red2">#ff0000</color>
<color name="green1">#336600</color>
<color name="text1">#000000</color>

and other is

<color name="grey1">#999999</color>
<color name="grey2">#333333</color>
<color name="grey3">#000000</color>
<color name="hover1">#8bb065</color>
<color name="red1">#ba0000</color>
<color name="red2">#ff0000</color>
<color name="green1">#336600</color>
<color name="text1">#ffffff</color>

these colors are used in layer list for background "activity_main_bg2.xml":

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="@color/grey1" />
        </shape>
    </item>
    <item
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp">
        <shape android:shape="rectangle" >
            <solid android:color="@color/grey2" />
        </shape>
     </item>
</layer-list>

My activity contains fragment:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world_dark"
    android:background="@drawable/activity_main_bg2" />

When I change time from day-night or back colors in background does not change. But if I use

android:background="@color/grey1"

everythings works ok.

How to solve this? Is this android bug?


回答1:


Please try this:

  1. Create file style.xml in folder res/values and res/values-night.
  2. Add to both of them style for your TextView like:

    <style name="textViewStyle">
    <item name="android:background">@drawable/activity_main_bg2</item>
    </style>
    
  3. Update your layout to:

    <TextView
    style="@style/textViewStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world_dark" />
    

This solution should get property color dependent of day/night mode.



来源:https://stackoverflow.com/questions/19380197/how-to-setup-night-mode-drawables-to-work-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!