【入门】Android自定义属性
2016-03-29 15:15
459 查看
学习笔记,高手请忽视
最近写了一个小项目,于是来此冒个泡,先总结下android自定义属性,下篇总结下自定义控件,然后简单的总结下自己学习的MVP架构,新手学习,先学应用,不求甚解。
自定义属性的应用简单的分为四步
一、自定义一个解析自定义属性的View类
如:
二、在工程res/values文件夹下新建声明属性的attrs.xml文件,属性声明的格式为:
下面是一个完整声明的例子
根据自定义的属性不同选取合适的format,format常用的类型有一下几种:
三、在布局文件中使用新属性
在使用之前必须要在布局文件中声明命名空间(xml name space简写为xmlns),格式如下:
使用自定义属性的布局文件如下:
四、在自定义的view的构造方法中,通过解析AttributeSet对象,获得所需属性。
列举了两种方法,选取其中任意一种即可
最近写了一个小项目,于是来此冒个泡,先总结下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(); } }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories