简体   繁体   中英

How to change the background/text color of a layout when daynight theme is used

I'm using Theme.AppCompat.DayNight and I want to customize a certain layout background and text color to change depending on whether it's day or night mode .

Here's what I tried so far:

<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/primaryDarkColorAmber</item>
    <item name="colorPrimaryDark">@color/primaryDarkColorAmber</item>
    <item name="colorControlActivated">@color/primaryDarkColorAmber</item>
</style>

<style name="PopUpTheme" parent="AppTheme">
    <!-- layout custom theme there's no darkBackground value...!! -->
    <item name="android:background">@color/popup_background</item>
</style>

首先创建 res/values-night 这个目录然后添加与 res/value 文件相同的文件,您可以复制并替换为颜色,然后当您更改白天或夜间主题时,您可以根据自己的风格获取颜色

If you are following Android's Color theming System , then you can use:

<View
...
android:background="?android:attr/colorBackground"
.../>

Similarly, for texts you can use ?android:attr/textColorPrimary and for icons ?attr/colorControlNormal . This way you do not need to create separate value files.

You can find more details here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM