在通过xml文件构造view组件的时候,往往都要使用到AttributeSet和defStyle这个两个参数,例如Button组件的构造方法Button(Context ctx, AttributeSet attrs, int defStyle)中,ctx会调用obtainStyledAttributes( AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes)方法获得一个TypedArray,然后根据这个TypeArray来设置组件的属性。obtainStyledAttributes这类方法有好几个,真正的实现是Resources.Theme类,分别是:
(1) obtainStyledAttributes( AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes) : TypedArray
根据attrs确定要获取哪些属性,然后依次通过其余3个参数来取得相应的属性值,属性值获取的优先级从高到低依次是set, defStyleAttr, defStyleRes. defStyleAttr是一个reference, 它指向当前Theme中的一个style, style其实就是各种属性的集合,如果defStyleAttr为0或者在Theme中没有找到相应的style, 则 才会尝试从defStyleRes获取属性值,defStyleRes表示的是一个style的id, 当它为0时也无效。
(2) obtainStyledAttributes( int resid, int[] attrs) : TypeArray
(3) obtainStyledAttributes(int[] attrs) : TypeArray
(2)和(3)分别表示从style或Theme里获取属性值。
R文件中会有styleable和attr这两个类,当我们要使用哪个属性集合或哪个属性的时候用的是styleable, 而attr类定义的仅仅是attr这个属性在layout中的id. AttributeSet有两个方法分别是
int getAttributeNameResource(int index);
int getAttributeResourceValue(int index, int defaultValue);
前一个方法获取的就是attr属性名称的id,也也就是attr类定义的数值,后一个方法获取的才是attr属性值。
自定义控件的AttributeSet属性步骤大致如下:
一、 首先要在res/values目录下建立一个attrs.xml(名字可以自己定义)的文件,并在此文件中增加对控件的属性的定义.其xml文件如下所示:
attrs.xml里通常包括若干个attr集合,例如
就表示一个attr集合,declare-styleable标签里的name值表示的就是上面方法里的attrs参数,android会自动在R文件中生成一个数组, 它可以使任意的不一定要是view组件名称。在集合里定义每个属性的名称和它的类型,自定义属性的Value值可以有10种类型以及其类型的组合值,如果允许多个类型可以用”|”来隔开,比如reference | color,
1. reference:参考某一资源ID。
(1)属性定义:
(2)属性使用:
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/图片ID"
/>
2. color:颜色值。
(1)属性定义:
(2)属性使用:
android:layout_width = "42dip"
android:layout_height = "42dip"
android:textColor = "#00FF00"
/>
3. boolean:布尔值。
(1)属性定义:
(2)属性使用:
二,自定义view中获取我们定义的属性
public MyView(Context context,AttributeSet attrs)
{
super(context,attrs);
mPaint = new Paint();
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.MyView);
int textColor = a.getColor(R.styleable.MyView_textColor,
0XFFFFFFFF);
float textSize = a.getDimension(R.styleable.MyView_textSize, 36);
mPaint.setTextSize(textSize);
mPaint.setColor(textColor);
a.recycle();
}
我们获取定义的属性我们R.sytleable.MyView_textColor, 获取方法中后面通常设定默认值(float textSize = a.getDimension(R.styleable.MyView_textSize, 36 ); ), 防止我们在xml 文件中没有定义.从而使用默认值!
获取,MyView 就是定义在
三、将我们自定义的MyView 加入布局main.xml 文件中,使用自定义属性,自定义属性必须加上:
xmlns:test =”http://schemas.android.com/apk/res/com.android.tutor “蓝色 是自定义属性的前缀,红色是我们包名.
下面是完整代码
1.自定义属性
2.自定义控件
package com.example.atrributesetdemo;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class MyView extends View{
private Paint paint;
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
//获取自定义属性数组
TypedArray array=context.obtainStyledAttributes(attrs,R.styleable.MyView);
//获得自定义字体颜色值
int color=array.getColor(R.styleable.MyView_text_color,0x123);
//获得自定义字体大小
float size=array.getDimension(R.styleable.MyView_text_size, 12);
//为画笔设置
paint=new Paint();
paint.setTextSize(size);
paint.setColor(color);
array.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRect(0, 0, 100, 100, paint);
canvas.drawText("今天学习自定义属性", 100, 100, paint);
}
}
3.布局引用
xmlns:my="http://schemas.android.com/apk/res/com.example.atrributesetdemo"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
android:layout_width="match_parent"
android:layout_height="match_parent"
my:text_color="#ff0000"
my:text_size="20sp"/>
4.activity中代码
package com.example.atrributesetdemo;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
更多深入http://blog.csdn.net/lmj623565791/article/details/45022631
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/145384.html