Android的主题 - 在自定义主题定义颜色主题、自定义、定义、颜色

由网友(青春无知又无辜)分享简介:我相信有一个简单的问题的答案但我只是无法找到它,所以我把它扔进计算器...; - )我只是把它变成一个例子。我有一个Android应用程序,用户可以选择主题在preferences - 或深或浅的主题。根据所选择的主题,我必须调整20种颜色在我的应用程序。所以,我希望我能在主题定义颜色,然后用在我的TextView...

我相信有一个简单的问题的答案但我只是无法找到它,所以我把它扔进计算器...; - )

我只是把它变成一个例子。我有一个Android应用程序,用户可以选择主题在preferences - 或深或浅的主题。根据所选择的主题,我必须调整20种颜色在我的应用程序。所以,我希望我能在主题定义颜色,然后用在我的TextViews等会这样定义颜色的名称但迄今为止我无法弄清楚如何做到这一点,并不能找到任何解决方案,在这里和那里。我真的不希望定义一个额外的深色和浅色风格每种颜色20咫尺天涯,这似乎是唯一的解决方案,我可以找到。

非常感谢任何提示

马丁:

更新:

在伪语法是我所期待的。这可能吗?

 <样式名称=AppTheme.MyDark父=安卓主题>
  ? -   - > titleColor =#FFFFFF
  ? -   - > introColor =#ffaaaa
< /风格>

<样式名称=AppTheme.MyLight父=机器人:Theme.Light>
  ? -   - > titleColor =#000000
  ? -   - > introColor =#004444
< /风格>


<的TextView
        机器人:ID =@ + ID / quoteTitle
        机器人:文字颜色= @ titleColor
        ...
< / TextView的>

<的TextView
        机器人:ID =@ + ID / quoteIntro
        机器人:文字颜色= @ introColor
        ...
< / TextView的>
 

解决方案

我发现了一个解决方案,它似乎工作。首先,你需要在attr.xml定义自定义颜色域

 < XML版本=1.0编码=UTF-8&GT?;
<资源>

< attr指示NAME =titleColor格式=引用|色/>
< attr指示NAME =introColor格式=引用|色/>

< /资源>
 
Android 12或将扩展主题系统,允许用户自定义app颜色

下面定义你的主题

 <样式名称=AppTheme.MyDark父=安卓主题>
   <项目名称=titleColor>#FFFFFF< /项目>
   <项目名称=introColor>#FFFFFF< /项目>
< /风格>


<样式名称=AppTheme.MyLight父=安卓主题>
   <项目名称=titleColor>#000000< /项目>
   <项目名称=introColor>#004444< /项目>
< /风格>
 

终于在布局

 <的TextView
    机器人:ID =@ + ID / quoteTitle
    机器人:titleColor文字颜色=
    ...
< / TextView的>

<的TextView
    机器人:ID =@ + ID / quoteIntro
    机器人:introColor文字颜色=
    ...
< / TextView的>
 

我找到了解决办法主要是here

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

I am sure there is a simple answer to that yet I just cant find it so I throw it into stackoverflow ... ;-)

I will just put it into an example. I have an android app where the user can choose the theme in the preferences - dark or light theme. Depending on the chosen theme I have to adjust 20 colors in my app. So I have the hope that I can define colours in the theme and then use the names of this so defined colours in the my TextViews etc. Yet so far I cant figure out how to do that and can't find any solution here and there. I really dont want to define an extra dark and light style for each of these 20 colours yet so far that seems the only solution I can find.

Big thanks for any hint

martin:

UPDATE:

In pseudo syntax is that is what I am looking for. Is it possible?

<style name="AppTheme.MyDark" parent="android:Theme">
  -?-> titleColor = "#ffffff"
  -?-> introColor = "#ffaaaa"  
</style>

<style name="AppTheme.MyLight" parent="android:Theme.Light">
  -?-> titleColor = "#000000"
  -?-> introColor = "#004444"  
</style>


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

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

解决方案

I found a solution which seems to work. First you need to 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.MyDark" parent="android:Theme">
   <item name="titleColor">#FFFFFF</item>
   <item name="introColor">#FFFFFF</item>
</style>


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

and finally in your layout

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

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

i found the solution mainly here

There seems to be no explanation in the official android documentation about using attributes. Best resource I found is here

阅读全文

相关推荐

最新文章