android自定义视图属性(atts.xml,TypedArray)学习
2013-09-02 00:24
661 查看
最近在学习过程中遇到这个问题,不知道TypedArray是干嘛用的?去官方网站看一下吧:TypedArray继承自Object类,然后再看下它的类概述:
[java] view
plaincopy
Container for an array of values that were retrieved with obtainStyledAttributes(AttributeSet, int[], int, int) or obtainAttributes(AttributeSet, int[]). Be sure to call recycle() when done with them. The indices used to retrieve values from this structure correspond to the positions of the attributes given to obtainStyledAttributes.
是一个用于存放恢复obtainStyledAttributes(AttributeSet,
int[], int, int)或
使用这个类的时候,先要在valuse文件夹下创建:atts.xml文件:
[html] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="FlowIndicator">
<attr name="count" format="integer" />
<attr name="space" format="dimension" />
<attr name="point_size" format="dimension" />
<attr name="point_seleted_color" format="color|reference" />
<attr name="point_normal_color" format="color|reference" />
<attr name="point_radius" format="dimension" />
</declare-styleable>
</resources>
首先,声明自定义<declare-styleable name="FlowIndicator">,nameFlowIndicator,属性设置为比较简单的格式,前面参数name,后面是参数格式。
自定义属性的format,可以有以下多种:
reference
string
color
dimension
boolean
integer
float
fraction
enum
flag
然后这样使用:
[java] view
plaincopy
public FlowIndicator(Context context, AttributeSet attrs) {
super(context, attrs);
//获得实例
TypedArray typeArray = context.obtainStyledAttributes(attrs,
R.styleable.FlowIndicator);
//从typeArray获取相应值,第二个参数为默认值,如第一个参数在atts.xml中没有定义,返回第二个参数值
count = typeArray.getInteger(R.styleable.FlowIndicator_count, 4);
space = typeArray.getDimension(R.styleable.FlowIndicator_space, 9);
radius = typeArray.getDimension(R.styleable.FlowIndicator_point_radius, 9);
point_normal_color = typeArray.getColor(
R.styleable.FlowIndicator_point_normal_color, 0x000000);
point_seleted_color = typeArray.getColor(
R.styleable.FlowIndicator_point_seleted_color, 0xffff07);
int sum = attrs.getAttributeCount();
if (Constans.DEBUG) {
String str = "";
for (int i = 0; i < sum; i++) {
String name = attrs.getAttributeName(i);
String value = attrs.getAttributeValue(i);
str += "attr_name :" + name + ": " + value + "\n";
}
Log.i("attribute", str);
}
typeArray.recycle();
}
最后一定不要忘记typeArray.recycle():
[java] view
plaincopy
Give back a previously retrieved StyledAttributes, for later re-use.
给回以前提取的styledattributes,以后再使用。
应该注意到,获取属性的时候所用的R.styleable.FlowIndicator_count中的FlowIndicator_count是采取的名字_属性这种格式。
定义好了自定义属性,就可以在自定控件中的属性设置了:
[html] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.dream.myqiyi"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.dream.myqiyi.widget.FlowIndicator
android:id="@+id/myView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
app:count="4"
android:gravity="center"
app:point_normal_color="#45000000"
app:point_radius="3dip"
app:point_seleted_color="#ffffff"
app:point_size="5dip"
app:space="10dip" />
</FrameLayout>
首先,要有声明:
xmlns:app="http://schemas.android.com/apk/res/com.dream.myqiyi",“com.dream.myqiyi”这个是你项目的包名。
然后我们就可以使用app:这样设置自定义的属性了。
[html] view
plaincopy
app:point_normal_color="#45000000"
app:point_radius="3dip"
app:point_seleted_color="#ffffff"
app:point_size="5dip"
app:space="10dip"
[java] view
plaincopy
Container for an array of values that were retrieved with obtainStyledAttributes(AttributeSet, int[], int, int) or obtainAttributes(AttributeSet, int[]). Be sure to call recycle() when done with them. The indices used to retrieve values from this structure correspond to the positions of the attributes given to obtainStyledAttributes.
是一个用于存放恢复obtainStyledAttributes(AttributeSet,
int[], int, int)或
obtainAttributes(AttributeSet, int[])值的一个数组容器,当操作完成以后,一定要调用recycle()方法。用于检索的索引值在这个结构对应的位置给obtainStyledAttributes属性。
使用这个类的时候,先要在valuse文件夹下创建:atts.xml文件:
[html] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="FlowIndicator">
<attr name="count" format="integer" />
<attr name="space" format="dimension" />
<attr name="point_size" format="dimension" />
<attr name="point_seleted_color" format="color|reference" />
<attr name="point_normal_color" format="color|reference" />
<attr name="point_radius" format="dimension" />
</declare-styleable>
</resources>
首先,声明自定义<declare-styleable name="FlowIndicator">,nameFlowIndicator,属性设置为比较简单的格式,前面参数name,后面是参数格式。
自定义属性的format,可以有以下多种:
reference
string
color
dimension
boolean
integer
float
fraction
enum
flag
然后这样使用:
[java] view
plaincopy
public FlowIndicator(Context context, AttributeSet attrs) {
super(context, attrs);
//获得实例
TypedArray typeArray = context.obtainStyledAttributes(attrs,
R.styleable.FlowIndicator);
//从typeArray获取相应值,第二个参数为默认值,如第一个参数在atts.xml中没有定义,返回第二个参数值
count = typeArray.getInteger(R.styleable.FlowIndicator_count, 4);
space = typeArray.getDimension(R.styleable.FlowIndicator_space, 9);
radius = typeArray.getDimension(R.styleable.FlowIndicator_point_radius, 9);
point_normal_color = typeArray.getColor(
R.styleable.FlowIndicator_point_normal_color, 0x000000);
point_seleted_color = typeArray.getColor(
R.styleable.FlowIndicator_point_seleted_color, 0xffff07);
int sum = attrs.getAttributeCount();
if (Constans.DEBUG) {
String str = "";
for (int i = 0; i < sum; i++) {
String name = attrs.getAttributeName(i);
String value = attrs.getAttributeValue(i);
str += "attr_name :" + name + ": " + value + "\n";
}
Log.i("attribute", str);
}
typeArray.recycle();
}
最后一定不要忘记typeArray.recycle():
[java] view
plaincopy
Give back a previously retrieved StyledAttributes, for later re-use.
给回以前提取的styledattributes,以后再使用。
应该注意到,获取属性的时候所用的R.styleable.FlowIndicator_count中的FlowIndicator_count是采取的名字_属性这种格式。
定义好了自定义属性,就可以在自定控件中的属性设置了:
[html] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.dream.myqiyi"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.dream.myqiyi.widget.FlowIndicator
android:id="@+id/myView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
app:count="4"
android:gravity="center"
app:point_normal_color="#45000000"
app:point_radius="3dip"
app:point_seleted_color="#ffffff"
app:point_size="5dip"
app:space="10dip" />
</FrameLayout>
首先,要有声明:
xmlns:app="http://schemas.android.com/apk/res/com.dream.myqiyi",“com.dream.myqiyi”这个是你项目的包名。
然后我们就可以使用app:这样设置自定义的属性了。
[html] view
plaincopy
app:point_normal_color="#45000000"
app:point_radius="3dip"
app:point_seleted_color="#ffffff"
app:point_size="5dip"
app:space="10dip"
相关文章推荐
- (转)android自定义视图属性(atts.xml,TypedArray)学习
- android自定义视图属性(atts.xml,TypedArray)学习
- 『ANDROID』android自定义视图属性(atts.xml,TypedArray)学习
- android自定义视图属性(atts.xml,TypedArray)学习
- Android高手之路之Android中的自定义属性attr.xml、TypedArray的使用
- Android学习之 自定义属性及TypedArray的用法
- Android自定义View中的自定义属性(attrs.xml,TypedArray的使用)
- Android自定义属性:attr.xml 与 TypedArray
- Android自定义视图一:扩展现有的视图,添加新的XML属性
- android自定义视图属性学习
- Android自定义View及自定义控件属性declare-styleable:自定义控件的属性(attr.xml,TypedArray)的使用
- Android自定义视图一:扩展现有的视图,添加新的XML属性
- android初级学习之视图的绘制(自定义View的实现原理)
- Android布局,xml,属性,TypedArray的一些事
- Android XML 中schema和自定义属性的关系
- Android如何自定义视图属性总结
- Android 中自定义控件和属性(attr.xml,declare-styleable,TypedArray)的方法和使用
- Android 中自定义控件和属性(attr.xml,declare-styleable,TypedArray)的方法和使用
- android学习7#--自定义View之自定义属性
- Android自定义视图和属性