Android自定义View添加自定义属性

前几天阿里的面试官问了我一个问题,如何实现自定义View的自定义属性,我第一感觉是很熟悉,但却答不上来。看来学习一定要学到它的原理。

下面进入正题。

1.自定义一个View类,这里我举个简单的例子,自定义TextView :

MyTextView.java

/** * 2015-3-21 */package com.example.attrtest;import android.annotation.SuppressLint;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Color;import android.util.AttributeSet;import android.widget.TextView;/** * @author wcy * */public class MyTextView extends TextView {public MyTextView(Context context) {super(context);}@SuppressLint("Recycle")public MyTextView(Context context, AttributeSet attrs) {super(context, attrs);}public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}}

2.将自定义的View类放到layout中,,仍然很简单<LinearLayout xmlns:android=""xmlns:tools=""android:layout_width="match_parent"android:layout_height="match_parent" ><com.example.attrtest.MyTextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="自定义属性" /></LinearLayout>

3.创建自定义属性

在/resalues/下新建attr.xml文件

<?xml version="1.0" encoding="utf-8"?><resources><declare-styleable name="MyTextView"><attr name="color" format="color" /><attr name="size" format="dimension" /></declare-styleable></resources>这里有一个域declare-styleable(声明属性),它有一个name属性MyTextView,这个name属性其实就是这个属性在R类中的id。这里有两个attr域,他们都有两个属性,name就不说了,format表示这个属性的类型,目前已知的属性有这些:

reference资源类型,通常是@开头,例如@+id

xx,@id

xxstring字符串类型,通常是文字信息dimension 浮点类型,通常是尺寸度量,单位有很多px,dp,sp等color 颜色类型,通常是颜色16进制代码,支持ARGBboolean 布尔类型,true和falseenum 枚举类型,通常是代表这个属性提供了几种值来进行选择,并且只能选择这几种中的一个flag 与enum基本没有区别integer 整数类型,通常是整数

4.在layout中添加自定义属性<LinearLayout xmlns:android=""xmlns:tools=""android:layout_width="match_parent"android:layout_height="match_parent" ><com.example.attrtest.MyTextViewxmlns:text=""android:layout_width="match_parent"android:layout_height="wrap_content"android:text="自定义属性"text:size="30sp"text:color="#FF0000" /></LinearLayout>注意:需要在自定义View标签下添加一个命名空间

xmlns:text=""

com.example.attrtest是包名。

5.在代码中实现自定义属性

/** * 2015-3-21 */package com.example.attrtest;import android.annotation.SuppressLint;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Color;import android.util.AttributeSet;import android.widget.TextView;/** * @author wcy * */public class MyTextView extends TextView {public MyTextView(Context context) {super(context);}@SuppressLint("Recycle")public MyTextView(Context context, AttributeSet attrs) {super(context, attrs);TypedArray t = getContext().obtainStyledAttributes(attrs,R.styleable.MyTextView);int textColor = t.getColor(R.styleable.MyTextView_color, Color.BLACK);float textSize = t.getDimension(R.styleable.MyTextView_size, 10);this.setTextColor(textColor);this.setTextSize(textSize);}public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}}使用getContext方法得到当前Context,调用Context.obtainStyledAttributes方法,传入AttributeSet 和R.styleable.MyTextView,这里的R.styleable.MyTextView,就是我们在attrs.xml中定义的名称,通过R.styleable来访问。

方法返回一个TypedArray对象。按照attrs,xml中定义的属性的类型,使用不同的get方法获取指定属性的值。

效果图

相信你已经学会了如何实现自定义View的自定义属性了。

别让别人徘徊的脚步踩碎你明天美好的梦想,

Android自定义View添加自定义属性

相关文章:

你感兴趣的文章:

标签云: