如何让我自己定义的属性值中,我的风格 [英] How to get my own defined attribute value in my style

查看:136
本文介绍了如何让我自己定义的属性值中,我的风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建3个不同的主题,使用自定义的(自己)属性的对话框。 我想加入到这个主题的风格,设置标题颜色: <项目名称=titleColor>#FF0000< /项目>

我的themes.xml:

 < XML版本=1.0编码=UTF-8&GT?;
<资源>
<样式名称=MyTheme的父=@安卓风格/主题>
    <项目名称=机器人:alertDialogStyle> @风格/对话框< /项目>
< /风格>
<样式名称=MyRedTheme父=MyTheme的>
    <项目名称=titleColor>#FF0000< /项目>
< /风格>
<样式名称=MyGreenTheme父=MyTheme的>
    <项目名称=titleColor>#00FF00< /项目>
< /风格>
<样式名称=MyBlueTheme父=MyTheme的>
    <项目名称=titleColor>#0000FF< /项目>
< /风格>
 

在attrs.xml

我定义titleColor属性:

 < XML版本=1.0编码=UTF-8&GT?;
<资源>
 <申报,设置样式名称=MyCustomAttributes>
  < attr指示NAME =titleColor格式=色|引用/>
 < /申报,设置样式>
< /资源>
 

我申请的主题对话之一。 我怎样才能把我的titleColor属性的值,以一个机器人:色?属性

 < XML版本=1.0编码=UTF-8&GT?;
<形状的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
       的xmlns:定制=htt​​p://schemas.android.com/apk/res/com.dicare
   机器人:形状=矩形>
       [固体机器人:颜色=我想通过titleColor价值在这里/>
< /形状>
 

解决方案

?titleColor <一个href="http://developer.android.com/guide/topics/resources/accessing-resources.html#ReferencesToThemeAttributes">see这里

您会定义颜色的colors.xml文件中引用它们像一个正常的资源:@​​彩色/ MyRed

您会成为您希望从布局XMLS定制你自己的看法自定义属性。例如,您可以延长的TextView写文本的第一行中的一种颜色(titleColor)比文本(机器人:文字颜色)的其余部分。

 &LT;颜色名称=MyRed&GT;#FF0000&LT; /彩色&GT;

&LT;样式名称=MyRedTheme父=MyTheme的&GT;
    &LT;项目名称=titleColor&GT; @色/ MyRed&LT; /项目&GT;
&LT; /风格&GT;

&LT;形状的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
       的xmlns:定制=htt​​p://schemas.android.com/apk/res/com.dicare
   机器人:形状=矩形&GT;
       [固体机器人:颜色=@色/ MyRed/&GT;
&LT; /形状&GT;
 

I want to create 3 different themes for a dialog using a custom (own) attribute. I would like to set title colors by adding this to theme's style: <item name="titleColor">#FF0000</item>

my themes.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="@android:style/Theme">
    <item name="android:alertDialogStyle">@style/dialog</item>
</style>
<style name="MyRedTheme" parent="MyTheme">
    <item name="titleColor">#FF0000</item>
</style>
<style name="MyGreenTheme" parent="MyTheme">
    <item name="titleColor">#00FF00</item>
</style>
<style name="MyBlueTheme" parent="MyTheme">
    <item name="titleColor">#0000FF</item>
</style>

I defined titleColor attribute in attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <declare-styleable name="MyCustomAttributes">
  <attr name="titleColor" format="color|reference" />
 </declare-styleable>
</resources>

I apply one of the themes for the dialog. How can I pass my titleColor attribute's value to an "android:color" attribute?

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:custom="http://schemas.android.com/apk/res/com.dicare"
   android:shape="rectangle">
       <solid android:color="I want to pass titleColor value here"/>
</shape>

解决方案

?titleColor see here

or

You'd define your colours in the colors.xml file and reference them like a normal resource: @color/MyRed

You'd make a custom attribute for your own views which you want to be customisable from layout xmls. For example, you might extend TextView to write the first line of text in one colour (titleColor) than the rest of the text (android:textColor).

<color name="MyRed">#FF0000</color>

<style name="MyRedTheme" parent="MyTheme">
    <item name="titleColor">@color/MyRed</item>
</style>

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:custom="http://schemas.android.com/apk/res/com.dicare"
   android:shape="rectangle">
       <solid android:color="@color/MyRed"/>
</shape>

这篇关于如何让我自己定义的属性值中,我的风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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