本文介绍了声明Android中设置样式属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关于声明-设置样式标记,使我们可以为组件声明自定义样式precious的小文件。我发现有效值这个列表格式属性在 ATTR 标记。虽然这是很好的,只要它去,它没有解释如何使用其中的一些价值观。浏览<一href="http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/res/res/values/attrs.xml;h=6370696708a2b7ecb561015629801bd380fe486a;hb=HEAD">attr.xml (Android的源标准属性),我发现,你可以做这样的事情:

 &LT;! - 最突出的文字颜色。 - &GT;
&LT; attr指示NAME =textColorPrimary格式=引用|色/&GT;
 

格式属性可以明显被设置为值的组合。 presumably的格式属性可帮助解析器间preT的实际样式值。后来我发现这attr.xml:

 &LT;! - 默认的文本字体。 - &GT;
&LT; attr指示NAME =字体&GT;
    &LT;枚举名=正常值=0/&GT;
    &LT;枚举名=SANS值=1/&GT;
    &LT;枚举名=衬线值=2/&GT;
    &LT;枚举名=等宽值=3/&GT;
&LT; / ATTR&GT;

&LT;! - 默认的文本字体样式。 - &GT;
&LT; attr指示NAME =文字样式&GT;
    &LT;标志名称=正常值=0/&GT;
    &LT;标志名称=黑体值=1/&GT;
    &LT;标志名称=斜体值=2/&GT;
&LT; / ATTR&GT;
 

这两个似乎声明一组允许值的指定样式。

所以,我有两个问题:

  1. 什么,可以采取一组枚举值,另一种可以在一组中的一个样式属性之间的区别标志值?
  2. 有谁知道的任何更好的文档如何声明-设置样式作品(除逆向工程的Andr​​oid源$ C ​​$ C)?
解决方案

还有这里的这个问题:定义定制ATTRS ,提供的部分的资料,但并不多。

这<一href="http://staticallytyped.word$p$pss.com/2011/04/16/android-custom-xml-attributes-and-views/">post 。它有大约国旗和枚举好消息:

在我看来,真正的价值,你可以在现实中加入由你可以从中获取有限的属性。此处检查的AttributeSet 类引用更多的提示。

您可以获取:

There's precious little documentation about the declare-styleable tag by which we can declare custom styles for components. I did find this list of valid values for the format attribute of the attr tag. While that's nice as far as it goes, it doesn't explain how to use some of those values. Browsing attr.xml (the Android source for the standard attributes), I discovered that you can do things like:

<!-- The most prominent text color.  -->
<attr name="textColorPrimary" format="reference|color" />

The format attribute can evidently be set to a combination of values. Presumably the format attribute helps the parser interpret an actual style value. Then I discovered this in attr.xml:

<!-- Default text typeface. -->
<attr name="typeface">
    <enum name="normal" value="0" />
    <enum name="sans" value="1" />
    <enum name="serif" value="2" />
    <enum name="monospace" value="3" />
</attr>

<!-- Default text typeface style. -->
<attr name="textStyle">
    <flag name="normal" value="0" />
    <flag name="bold" value="1" />
    <flag name="italic" value="2" />
</attr>

Both of these seem to declare a set of allowed values for the indicated style.

So I have two questions:

  1. What's the difference between a style attribute that can take on one of a set of enum values and one that can take on a set of flag values?
  2. Does anyone know of any better documentation for how declare-styleable works (other than reverse engineering the Android source code)?
解决方案

There's this question here: Defining custom attrs with some info, but not much.

And this post . It has good info about flags and enums:

As I see it, the real values you can add in reality to an attribute is limited by what you can obtain from it. Check the AttributeSet class reference here for more hints.

You can obtain:

  • booleans (getAttributeBooleanValue),
  • floats (getAttributeFloatValue),
  • ints (getAttributeIntValue),
  • ints (as getAttributeUnsignedIntValue),
  • and strings (getAttributeValue)

这篇关于声明Android中设置样式属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 23:16
查看更多