【入门】Android自定义属性

来源:互联网 发布:spark sql groupbykey 编辑:程序博客网 时间:2024/06/06 05:19

学习笔记,高手请忽视

最近写了一个小项目,于是来此冒个泡,先总结下android自定义属性,下篇总结下自定义控件,然后简单的总结下自己学习的MVP架构,新手学习,先学应用,不求甚解。

自定义属性的应用简单的分为四步

一、自定义一个解析自定义属性的View类

如:

public class MyView extends View{public MyView(Context context){super(context);}/** * 在布局中没有使用自定义属性时,默认调用两个参数的构造方法 *  * @param context * @param attrs */public MyView(Context context, AttributeSet attrs){super(context, attrs);}/** * 当使用了自定义属性时调用此构造方法 *  * @param context * @param attrs * @param defStyle */public MyView(Context context, AttributeSet attrs, int defStyle){super(context, attrs, defStyle);}}

二、在工程res/values文件夹下新建声明属性的attrs.xml文件,属性声明的格式为:

<attr name="属性名" format="数据类型"/>

下面是一个完整声明的例子

<declare-styleable name="MyView">        <attr name="grade_id" format="integer"/></declare-styleable> 

根据自定义的属性不同选取合适的format,format常用的类型有一下几种:
reference  引用color      颜色boolean    布尔值dimension  尺寸float      浮点integer    整型值string     字符串enum       枚举值

三、在布局文件中使用新属性

在使用之前必须要在布局文件中声明命名空间(xml name space简写为xmlns),格式如下:

xmlns:example="http://schemas.android.com/apk/res/com.example.customwidget"固定   任意          固定前缀                    |应用包名
注意:上面格式中注释的是应用包名,千万不要写成类的路径

使用自定义属性的布局文件如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:myview="http://schemas.android.com/apk/res/com.example.customwidget"    android:layout_width="match_parent"    android:layout_height="match_parent">    <com.example.customwidget.MyView        android:layout_width="40dp"        android:layout_height="40dp"        android:background="#ff0000"        myview:grade_id="1" />    <com.example.customwidget.MyView        android:layout_width="40dp"        android:layout_height="40dp"        android:background="#00ff00"        myview:grade_id="2" />    <com.example.customwidget.MyView        android:layout_width="40dp"        android:layout_height="40dp"        android:background="#0000ff"        myview:grade_id="3" /></RelativeLayout>

四、在自定义的view的构造方法中,通过解析AttributeSet对象,获得所需属性。

列举了两种方法,选取其中任意一种即可

public class MyView extends View{private int gradeId;public MyView(Context context){super(context);}/** * 在布局中没有使用自定义属性时,默认调用两个参数的构造方法 *  * @param context * @param attrs */public MyView(Context context, AttributeSet attrs){super(context, attrs);/********************************************* 取出属性方法一 *************************************************/TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyView, 0, 0);try{gradeId = array.getInteger(R.styleable.MyView_grade_id, 0);} finally{/** 要记得释放资源 */array.recycle();}}/** * 当使用了自定义属性时调用此构造方法 *  * @param context * @param attrs * @param defStyle */public MyView(Context context, AttributeSet attrs, int defStyle){super(context, attrs, defStyle);/********************************************* 取出属性方法二 *************************************************/TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyView, defStyle, 0);int count = a.getIndexCount();for (int i = 0; i < count; i++){int attr = a.getIndex(i);switch (attr){case R.styleable.MyView_grade_id:/** 这样就可以把属性值取出来了 */gradeId = a.getDimensionPixelSize(attr, 0);break;}}/** 要记得释放资源 */a.recycle();}}



1 0