安卓自定义View(一)自定义控件属性

自定义View增加属性第一步:定义属性资源文件

在/res/values 文件夹下建立“Values XML layout”,按照如下定义一个textview的属性

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="name" format="reference"/>
    <attr name="sex" format="reference"/>
    <attr name="age" format="integer"/>
    <attr name="textColor" format="color"/>
    <attr name="textSize" format="dimension"/>
    <declare-styleable name="HsTextView">
        <attr name="name"/>
        <attr name="sex"/>
        <attr name="age"/>
        <attr name="textColor"/>
        <attr name="textSize"/>
    </declare-styleable>
</resources>

节点attr 是定义属性用的,name 是属性名称 ,format 是属性的类型,用<declare-styleable name="HsTextView">来给定义的属性命名,这里在layout里面需要引用这个名字。

另外除了reference与integer color dimension属性以外还有如下属性:

1. float:浮点值。

3. string:字符串

4. fraction:百分数。

5. enum:枚举值

6. flag:是自己定义的,类似于 android:gravity="top",就是里面对应了自己的属性值。

7. reference|color:颜色的资源文件。

8.reference|boolean:布尔值的资源文件

dimension主要是sp与dp属性,在获取的时候需要转换成像素值。reference是资源文件,在layout中设定值的时候需要用"@.."来引入资源文件。不能直接设定,设定完毕之后在layout中如此定义:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:hsTextViewattr="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
       <com.skymaster.hs.messagetest.HsTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        hsTextViewattr:name = "@string/name"
        hsTextViewattr:sex="@string/sex"
       />
 </LinearLayout>

这里要提示一点,xmlns:hsTextViewattr="http://schemas.android.com/apk/res-auto" 这里是指定命名空间,xmlns:XML namespace, 指定的名字是hsTextViewattr,后面要使用这个属性的时候就用“hsTextViewattr:name = "@string/name"”,系统的命名空间是android所以都是"android:layout_width...",有一点需要说明的是在eclipse中这个命名空间的写法是:
  xmlns:你自己定义的名称=http://schemas.android.com/apk/res/你程序的package包名

而在android studio中只要改成"res-auto"这样就方便很多了

自定义View增加属性第二步:在构造函数里获取属性

定义一个自己的View并且继承TextView,重写构造方法获取自定义的属性:

public class HsTextView extends TextView {
    private static final String TAG = "HsTextView";
    private final String DEFAULT_TEXT_NAME = "默认姓名";
    private final int DEFAULT_TEXT_AGE = 100;
    private final String DEFAULT_TEXT_SEX = "男";
    private final int DEFAULT_TEXT_SIZE = 10;//sp
    private final int DEFAULT_TEXT_COLOR = 0xff00c0c0;

    private String mTextName = DEFAULT_TEXT_NAME;
    private int mTextAge = DEFAULT_TEXT_AGE;
    private String mTextSex = DEFAULT_TEXT_SEX;
    private int mTextColor = DEFAULT_TEXT_COLOR;
    private float mTextSize = sp2px(DEFAULT_TEXT_SIZE);

    public HsTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        obtainStyleAttrs(attrs);
    }

    private void obtainStyleAttrs(AttributeSet attrs) {
        TypedArray a = getContext().obtainStyledAttributes(attrs,R.styleable.HsTextView);
        mTextName = a.getString(R.styleable.HsTextView_name);
        mTextSex = a.getString(R.styleable.HsTextView_sex);
        mTextAge = a.getInteger(R.styleable.HsTextView_age,mTextAge);
        mTextColor = a.getColor(R.styleable.HsTextView_textColor,mTextColor);
        mTextSize = (int) a.getDimension(R.styleable.HsTextView_textSize,mTextSize);
        setTextColor(mTextColor);
        setTextSize(mTextSize);
        setText("姓名:"+mTextName+"\n"+"年龄:"+mTextAge+"\n"+"性别:"+mTextSex);
        a.recycle();
    }

    public HsTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }
    private float sp2px(int spVal){
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,spVal,
                getResources().getDisplayMetrics());
    }
}

通过xml实现空间对象默认调用的是2个参数的构造函数也就是  public HsTextView(Context context, AttributeSet attrs)

这里传入了上下文与AttribueSet,从字面理解就是属性集合,通过Context的obtainStyledAttributes(..)获取属性数组,记得这里要定义的TypeArray使用完之后一定要释放a.recycle();另外一个需要注意的是定义字体大小定义的属性是sp需要转换成像素

所以要使用工具类实现:

sp2px: TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,spVal,getResources().getDisplayMetrics());基本是固定写法。另外dp转px的写法:

dp2px:TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dpVal, getResources().getDisplayMetrics());

这里返回的都是float,根据需要进行类型强制转换。

完成之后就可以显示了自己定义的属性了:

 

 引用博客地址http://blog.csdn.net/vipzjyno1/article/details/23696537

posted @ 2016-12-01 10:34  麦哈顿博士  阅读(528)  评论(0编辑  收藏  举报