设置基于主题颜色颜色、主题

2023-09-04 03:13:07 作者:爱情是一份甜蜜の忧伤

我有我经历了我的布局使用强调色。我把它定义为我申请一个TextView的样式。

I have an accent color that I use through out my layouts. I have it defined as a style that I apply to a TextView.

我的应用程序可以让一个黑暗的主题和光的主题之间的用户选择。我想调整的基础上选定的主题强调色。

My application lets the user choose between a dark theme and a light theme. I would like to tweak the accent color based on the selected theme.

您不能定义在一个主题颜色 您不能根据主题定义状态列表颜色 您可以根据主题不是单独的资源(因为你可以版本或屏幕大小) 您不能更新从code(???这个???不是100%)资源彩

我应该如何控制自己的基础上,选定的主题强调色?

How should I control my accent color based on the selected theme?

的开发指南提供了使用自定义的颜色,这是接近我想要一个主题的例子。我需要能够改变颜色在运行时。我知道我可以进入我的code并在这种风格被使用,我可以FERRIT出来的组件,并设置了颜色的方式在X的地方。但我认为我能与风格/主题的某种组合实现这一点。

The dev guide gives an example of a theme using a custom color, which is close to what I want. I need to be able to change the color at runtime. I know I could go into my code and the X places where this style is being used I could ferrit out the component and set the color that way. But I would think I could accomplish this with some combination of style/theme.

感谢

推荐答案

我同样的问题,并努力接近做类似的事情是什么,你想出但幸运的是我发现了以下的解决方案,我还指出here.

I struggled with the same question and was close to do something similar to what you came up with yet luckily i found the following solution which I also stated here.

首先,您在attr.xml定义自定义颜色域

First you define the custom color fields in attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

<attr name="titleColor" format="reference|color" />
<attr name="introColor" format="reference|color" />

</resources>

下面定义你的主题

Next you define your themes

<style name="AppTheme.MyLight" parent="android:Theme">
   <item name="titleColor">#FFFFFF</item>
   <item name="introColor">#FFFFAA</item>
</style>


<style name="AppTheme.MyDark" parent="android:Theme">
   <item name="titleColor">#000000</item>
   <item name="introColor">#004444</item>
</style>

终于在布局

<TextView
    android:id="@+id/quoteTitle"
    android:textColor="?titleColor"
    ...
</TextView>

<TextView
    android:id="@+id/quoteIntro"
    android:textColor="?introColor"
    ...
</TextView>

似乎有关于使用属性Android官方文档中没有解释。我发现最好的资源就是这里。

 
精彩推荐
图片推荐