如何设置夜间模式可绘制对象按预期工作
我要在夜间模式更改时更改背景。
I what to change background as night mode changes.
我有/ values和/ values-night文件夹,其中包含具有不同值的 colors.xml。
`
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>
,其他是
<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>
这些颜色用于图层背景 activity_main_bg2.xml的列表:
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"
一切正常。
如何解决?
请尝试以下操作:
- 在res / values文件夹和res / values-night中创建文件style.xml。
-
为您的TextView添加两种样式:
- Create file style.xml in folder res/values and res/values-night.
Add to both of them style for your TextView like:
<style name="textViewStyle">
<item name="android:background">@drawable/activity_main_bg2</item>
</style>
将布局更新为:
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.