定义自定义ATTRS [英] Defining custom attrs
问题描述
我需要实现我自己的属性,就像在com.android.R.attr
I need to implement my own attributes like in com.android.R.attr
在官方文档中什么也没找到,所以我需要如何定义这些ATTRS以及如何使用它们从我的code的信息。
Found nothing in official documentation so I need information about how to define these attrs and how to use them from my code.
感谢
推荐答案
目前最好的文档源。你可以看看它<一个href="https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/attrs.xml">here (attrs.xml)。
Currently the best documentation is the source. You can take a look at it here (attrs.xml).
您可以定义在顶级&LT属性;申报,设置样式&GT;
&LT的元素或内部;资源&GT >元素。如果我想使用ATTR在多个地方,我把它的根元素。请注意,所有的属性共享同一个全局命名空间。这意味着,即使你创建内部的新属性的&LT;申报,设置样式&GT;
元素,它可以在外部无法使用,你不能创建同名的另一个属性不同类型的
You can define attributes in the top <resources>
element or inside of a <declare-styleable>
element. If I'm going to use an attr in more than one place I put it in the root element. Note, all attributes share the same global namespace. That means that even if you create a new attribute inside of a <declare-styleable>
element it can be used outside of it and you cannot create another attribute with the same name of a different type.
这是&LT; attr指示&GT;
元素有两个XML属性名称
和格式
。 名称
,您可以调用它的东西,这是你最后提到它在code,例如, R.attr.my_attribute
。该格式
属性可以有不同的值取决于类型的属性你想要的。
An <attr>
element has two xml attributes name
and format
. name
lets you call it something and this is how you end up referring to it in code, e.g., R.attr.my_attribute
. The format
attribute can have different values depending on the 'type' of attribute you want.
- 参考 - 如果它引用了其他资源的ID(例如,@色/ my_color的,@布局/ my_layout)
- 颜色
- 在布尔
- 尺寸
- 浮动
- 在整数
- 字符串
- 分数
- 枚举 - 通常隐含定义
- 标记 - 通常隐含定义
- reference - if it references another resource id (e.g, "@color/my_color", "@layout/my_layout")
- color
- boolean
- dimension
- float
- integer
- string
- fraction
- enum - normally implicitly defined
- flag - normally implicitly defined
您可以通过使用格式设置为多种类型的 |
,例如格式=引用|色
You can set the format to multiple types by using |
, e.g., format="reference|color"
.
枚举
属性可以定义如下:
<attr name="my_enum_attr">
<enum name="value1" value="1" />
<enum name="value2" value="2" />
</attr>
标志
属性类似,只是数值需要加以界定,使他们能够位或在一起:
flag
attributes are similar except the values need to be defined so they can be bit ored together:
<attr name="my_flag_attr">
<flag name="fuzzy" value="0x01" />
<flag name="cold" value="0x02" />
</attr>
在除了属性,还有就是&LT;申报,设置样式&GT;
元素。这允许您定义属性的自定义视图可以使用。您可以通过指定&LT; attr指示&GT;
元素,如果它是previously定义你不指定格式
。如果你想重新使用一个机器人ATTR,例如机器人:重力,那么你可以做,在名称
,如下:
In addition to attributes there is the <declare-styleable>
element. This allows you to define attributes a custom view can use. You do this by specifying an <attr>
element, if it was previously defined you do not specify the format
. If you wish to reuse an android attr, for example, android:gravity, then you can do that in the name
, as follows.
自定义视图的一个例子&LT;申报,设置样式&GT;
:
An example of a custom view <declare-styleable>
:
<declare-styleable name="MyCustomView">
<attr name="my_custom_attribute" />
<attr name="android:gravity" />
</declare-styleable>
当定义你的自定义属性在XML中的自定义视图,你需要做一些事情。首先,你必须声明一个命名空间来找到你的属性。你这样做的根布局元素。通常情况下,只有的xmlns:机器人=http://schemas.android.com/apk/res/android
。现在,您还必须添加的xmlns:无论=http://schemas.android.com/apk/res-auto
When defining your custom attributes in XML on your custom view you need to do a few things. First you must declare a namespace to find your attributes. You do this on the root layout element. Normally there is only xmlns:android="http://schemas.android.com/apk/res/android"
. You must now also add xmlns:whatever="http://schemas.android.com/apk/res-auto"
.
例如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:whatever="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<org.example.mypackage.MyCustomView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
whatever:my_custom_attribute="Hello, world!" />
</LinearLayout>
最后,访问自定义属性通常你这样做你的自定义视图的构造函数如下:
Finally, to access that custom attribute you normally do so in the constructor of your custom view as follows.
public MyCustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView, defStyle, 0);
String str = a.getString(R.styleable.MyCustomView_my_custom_attribute);
//do something with str
a.recycle();
}
结束。 :)
The end. :)
这篇关于定义自定义ATTRS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!