机器人:我如何定义自定义颜色,可绘制等在主题? [英] android: How can I define custom colors, drawables, etc. in themes?

查看:181
本文介绍了机器人:我如何定义自定义颜色,可绘制等在主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我可以解释我后。从本质上说,我的用户都问我,让不同的外观在我的应用程序,我希望我能和主题做。

I hope I can explain what I'm after. In essence, my users have asked me to allow different looks in my application, which I hope I can do with themes.

我希望我可以做这样的事情:

I hoped I could do something like this:

<style name="NewTheme" parent="android:Theme.Dark">
  <item name="labelColor">#f90</item>
  <item name="buttonColor">#fff</item>
  <item name="buttonBg">@drawable/button</item>
</style>
<style name="OldTheme" parent="android:Theme.Dark">
  <item name="labelColor">#fa0</item>
  <item name="buttonColor">#88f</item>
  <item name="buttonBg">@drawable/button_old</item>
</style>

和我的styles.xml然后引用这些值:

And then reference these values in my styles.xml:

<style name="labelStyle">
  <item name="android:textColor>@labelColor</item>
</style>
<style name="buttonStyle">
  <item name="android:textcolor">@buttonColor</item>
  <item name="android:background">@buttonBg</item>
</style>

我知道这个语法是错误的,但什么可能是正确的语法?基本上,我想创建属性集(颜色,背景,一对夫妇的其他东西),并根据主题选择它们。

I know this syntax is wrong, but what might be the right syntax? Basically, I want to create sets of attributes (color, background, a couple other things) and select them based on theme.

推荐答案

要与你有主题和风格在Android的工作:

To work with themes and styles in Android you have to:

  1. 定义一个或多个主题中的themes.xml和设定的定义 您的样式那里。

  1. Define one or more themes in themes.xml and set the definitions of your styles there.

定义自定义属性,又名自定义样式,在attrs.xml。

Define custom attributes, a.k.a. custom styles, in attrs.xml.

描述一下您的自定义样式值在styles.xml。

Describe what the values of your custom styles are in styles.xml.

在你的布局文件,给你的意见样式属性,其中有一个 自定义样式的名称作为自己的价值。

In your layout files, give your views a style attribute, which has a custom style name as their values.

设置应用程序的主题或活动中的任一 AndroidManifest.xml中或在活动的onCreate()。这是通过在活动的onCreate()方法调用setTheme()完成后,任何调用的setContentView()之前

Set the theme of your application or activity in either AndroidManifest.xml or in the Activity's onCreate(). This is done by calling setTheme() in the activity's onCreate() method, before any call to setContentView().

要更改主题,你只需要重新启动您的活动。

To change the theme, you simply need to restart your activity.

Iadvice你看看这教程它处理所有的程序员要工作在Android主题(文本颜色,文本格式,状态列表绘制等...)

Iadvice you to look at this tutorial it deals with all that a programmer want to work on android themes (text color, text formatting, state list drawable etc ...)

这篇关于机器人:我如何定义自定义颜色,可绘制等在主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆