Android 深入理解Android中的自定义属性
转载请标明出处:
http://blog.csdn.net/lmj623565791/article/details/45022631;
本文出自:【张鸿洋的博客】
1、引言
对于自定义属性,大家肯定都不陌生,遵循以下几步,就可以实现:
- 自定义一个CustomView(extends View )类
- 编写values/attrs.xml,在其中编写styleable和item等标签元素
- 在布局文件中CustomView使用自定义的属性(注意namespace)
- 在CustomView的构造方法中通过TypedArray获取
ps:如果你对上述几个步骤不熟悉,建议先熟悉下,再继续~
那么,我有几个问题:
- 以上步骤是如何奏效的?
- styleable 的含义是什么?可以不写嘛?我自定义属性,我声明属性就好了,为什么一定要写个styleable呢?
- 如果系统中已经有了语义比较明确的属性,我可以直接使用嘛?
- 构造方法中的有个参数叫做
AttributeSet
(eg: MyTextView(Context context, AttributeSet attrs) )这个参数看名字就知道包含的是参数的数组,那么我能不能通过它去获取我的自定义属性呢? - TypedArray是什么鬼?从哪冒出来的,就要我去使用?
恩,针对这几个问题,大家可以考虑下,如何回答呢?还是说:老子会背上述4个步骤就够了~~
2、常见的例子
接下来通过例子来回答上述问题,问题的回答顺序不定~~大家先看一个常见的例子,即上述几个步骤的代码化。
- 自定义属性的声明文件
<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="test"><attr name="text" format="string" /><attr name="testAttr" format="integer" /></declare-styleable></resources>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
![](http://static.blog.csdn.net/images/save_snippets.png)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 自定义View类
package com.example.test;import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;public class MyTextView extends View {private static final String TAG = MyTextView.class.getSimpleName();public MyTextView(Context context, AttributeSet attrs) {super(context, attrs);TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.test);String text = ta.getString(R.styleable.test_testAttr);int textAttr = ta.getInteger(R.styleable.test_text, -1);Log.e(TAG, "text = " + text + " , textAttr = " + textAttr);ta.recycle();}}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
![](http://static.blog.csdn.net/images/save_snippets.png)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 布局文件中使用
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:zhy="http://schemas.android.com/apk/res/com.example.test"android:layout_width="match_parent"android:layout_height="match_parent" ><com.example.test.MyTextView
android:layout_width="100dp"android:layout_height="200dp"zhy:testAttr="520"zhy:text="helloworld" /></RelativeLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
![](http://static.blog.csdn.net/images/save_snippets.png)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
ok,大家花3s扫一下,运行结果为:
MyTextView: text = helloworld , textAttr = 520
- 1
![](http://static.blog.csdn.net/images/save_snippets.png)
- 1
应该都不意外吧,注意下,我的styleable的name写的是test,所以说这里并不要求一定是自定义View的名字。
3、AttributeSet与TypedArray
下面考虑:
构造方法中的有个参数叫做
AttributeSet
(eg: MyTextView(Context context, AttributeSet attrs) )这个参数看名字就知道包含的是参数的集合,那么我能不能通过它去获取我的自定义属性呢?
首先AttributeSet
中的确保存的是该View声明的所有的属性,并且外面的确可以通过它去获取(自定义的)属性,怎么做呢?
其实看下AttributeSet
的方法就明白了,下面看代码。
public MyTextView(Context context, AttributeSet attrs) {super(context, attrs);int count = attrs.getAttributeCount();for (int i = 0; i < count; i++) {String attrName = attrs.getAttributeName(i);String attrVal = attrs.getAttributeValue(i);Log.e(TAG, "attrName = " + attrName + " , attrVal = " + attrVal);}// ==>use typedarray ...}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
![](http://static.blog.csdn.net/images/save_snippets.png)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
输出:
MyTextView(4136): attrName = layout_width , attrVal = 100.0dip
MyTextView(4136): attrName = layout_height , attrVal = 200.0dip
MyTextView(4136): attrName = text , attrVal = helloworld
MyTextView(4136): attrName = testAttr , attrVal = 520
- 1
- 2
- 3
- 4
- 5
![](http://static.blog.csdn.net/images/save_snippets.png)
- 1
- 2
- 3
- 4
- 5
结合上面的布局文件,你发现了什么?
我擦,果然很神奇,真的获得所有的属性,恩,没错,通过AttributeSet
可以获得布局文件中定义的所有属性的key和value(还有一些方法,自己去尝试),那么是不是说TypedArray这个鬼可以抛弃了呢?答案是:NO!
。
现在关注下一个问题:
TypedArray是什么鬼?从哪冒出来的,就要我去使用?
我们简单修改下,布局文件中的MyTextView的属性。
<com.example.test.MyTextView
android:layout_width="@dimen/dp100"android:layout_height="@dimen/dp200"zhy:testAttr="520"zhy:text="@string/hello_world" />
- 1
- 2
- 3
- 4
- 5
![](http://static.blog.csdn.net/images/save_snippets.png)
- 1
- 2
- 3
- 4
- 5
现在再次运行的结果是:
MyTextView(4692): attrName = layout_width , attrVal = @2131165234
MyTextView(4692): attrName = layout_height , attrVal = @2131165235
MyTextView(4692): attrName = text , attrVal = @2131361809
MyTextView(4692): attrName = testAttr , attrVal = 520
>>use typedarray
MyTextView(4692): text = Hello world! , textAttr = 520
- 1
- 2
- 3
- 4
- 5
- 6
![](http://static.blog.csdn.net/images/save_snippets.png)
- 1
- 2
- 3
- 4
- 5
- 6
发现了什么?通过AttributeSet
获取的值,如果是引用都变成了@+数字的字符串。你说,这玩意你能看懂么?那么你看看最后一行使用TypedArray获取的值,是不是瞬间明白了什么。
TypedArray
其实是用来简化我们的工作的,比如上例,如果布局中的属性的值是引用类型(比如:@dimen/dp100),如果使用AttributeSet
去获得最终的像素值,那么需要第一步拿到id,第二步再去解析id。而TypedArray正是帮我们简化了这个过程。
贴一下:如果通过AttributeSet
获取最终的像素值的过程:
int widthDimensionId = attrs.getAttributeResourceValue(0, -1);Log.e(TAG, "layout_width= "+getResources().getDimension(widthDimensionId));
- 1
- 2
- 3
![](http://static.blog.csdn.net/images/save_snippets.png)
- 1
- 2
- 3
ok,现在别人问你TypedArray存在的意义,你就可以告诉他了。
4、declare-styleable
我们已经解决了两个问题,接下来,我们看看布局文件,我们有一个属性叫做:zhy:text
。
总所周知,系统提供了一个属性叫做:Android:text
,那么我觉得直接使用android:text
更nice,这样的话,考虑问题:
如果系统中已经有了语义比较明确的属性,我可以直接使用嘛?
答案是可以的,怎么做呢?
直接在attrs.xml中使用android:text
属性。
<declare-styleable name="test"><attr name="android:text" /><attr name="testAttr" format="integer" /></declare-styleable>
- 1
- 2
- 3
- 4
![](http://static.blog.csdn.net/images/save_snippets.png)
- 1
- 2
- 3
- 4
注意,这里我们是使用已经定义好的属性,不需要去添加format
属性(注意声明和使用的区别,差别就是有没有format)。
然后在类中这么获取:ta.getString(R.styleable.test_android_text)
;布局文件中直接android:text="@string/hello_world"
即可。
这里提一下,系统中定义的属性,其实和我们自定义属性的方式类似,你可以在sdk/platforms/android-xx/data/res/values
该目录下看到系统中定义的属性。然后你可以在系统提供的View(eg:TextView)的构造方法中发现TypedArray获取属性的代码(自己去看一下)。
ok,接下来,我在想,既然declare-styleable
这个标签的name都能随便写,这么随意的话,那么考虑问题:
styleable 的含义是什么?可以不写嘛?我自定义属性,我声明属性就好了,为什么一定要写个styleable呢?
其实的确是可以不写的,怎么做呢?
- 首先删除declare-styleable的标签
那么现在的attrs.xml为:
<?xml version="1.0" encoding="utf-8"?>
<resources><attr name="testAttr" format="integer" />
</resources>
- 1
- 2
- 3
- 4
![](http://static.blog.csdn.net/images/save_snippets.png)
- 1
- 2
- 3
- 4
哟西,so清爽~
* MyTextView实现
package com.example.test;import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;public class MyTextView extends View {private static final String TAG = MyTextView.class.getSimpleName();private static final int[] mAttr = { android.R.attr.text, R.attr.testAttr };private static final int ATTR_ANDROID_TEXT = 0;private static final int ATTR_TESTATTR = 1;public MyTextView(Context context, AttributeSet attrs) {super(context, attrs);// ==>use typedarrayTypedArray ta = context.obtainStyledAttributes(attrs, mAttr);String text = ta.getString(ATTR_ANDROID_TEXT);int textAttr = ta.getInteger(ATTR_TESTATTR, -1);//输出 text = Hello world! , textAttr = 520Log.e(TAG, "text = " + text + " , textAttr = " + textAttr);ta.recycle();}}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
![](http://static.blog.csdn.net/images/save_snippets.png)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
貌似多了些代码,可以看到我们声明了一个int数组,数组中的元素就是我们想要获取的attr的id。并且我们根据元素的在数组中的位置,定义了一些整形的常量代表其下标,然后通过TypedArray
进行获取。
可以看到,我们原本的:
R.styleable.test => mAttr
R.styleable.test_text => ATTR_ANDROID_TEXT(0)
R.styleable.test_testAttr => ATTR_TESTATTR(1)
- 1
- 2
- 3
![](http://static.blog.csdn.net/images/save_snippets.png)
- 1
- 2
- 3
那么其实呢?android在其内部也会这么做,按照传统的写法,它会在R.Java生成如下代码:
public static final class attr {public static final int testAttr=0x7f0100a9;}
public static final class styleable {public static final int test_android_text = 0;public static final int test_testAttr = 1;public static final int[] test = {0x0101014f, 0x7f0100a9};}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
![](http://static.blog.csdn.net/images/save_snippets.png)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
ok,根据上述你应该发现了什么。styleale的出现系统可以为我们完成很多常量(int[]数组,下标常量)等的编写,简化我们的开发工作(想想如果一堆属性,自己编写常量,你得写成什么样的代码)。那么大家肯定还知道declare-styleable
的name属性,一般情况下写的都是我们自定义View的类名。主要为了直观的表达,该declare-styleable
的属性,都是改View所用的。
其实了解该原理是有用的,详见:Android 自定义控件 优雅实现元素间的分割线
ok,现在5个问题,回答了4个,第一个问题:
自定义属性的几个步骤是如何奏效的?
恩,上述以及基本涵盖了这个问题的答案,大家自己总结,所以:略。
总结下今天的博客。
- attrs.xml里面的declare-styleable以及item,android会根据其在R.java中生成一些常量方便我们使用(aapt干的),本质上,我们可以不声明declare-styleable仅仅声明所需的属性即可。
- 我们在View的构造方法中,可以通过
AttributeSet
去获得自定义属性的值,但是比较麻烦,而TypedArray
可以很方便的便于我们去获取。 - 我们在自定义View的时候,可以使用系统已经定义的属性。
近期的更新计划:自定义View的一些细节相关的Blog(重点会在交互上),Android最佳实践相关的文章,framework相关的一些文章,敬请期待。
群号:423372824
微信公众号:hongyangAndroid
(欢迎关注,第一时间推送博文信息)
Android 深入理解Android中的自定义属性相关推荐
- Android版本dp,Android中sp和dp区别
--- title: Android中sp和dp区别 date: 2016-09-08 17:04:15 tags: Android categories: Android开发 --- ## 理解an ...
- android开发:Android 中自定义属性(attr.xml,TypedArray)的使用
今天我们的教程是根据前面一节扩展进行的,如果你没有看,请点击 Android高手进阶教程(三)查看第三课,这样跟容易方便你的理解! 在xml 文件里定义控件的属性,我们已经习惯了android:att ...
- 【转】Android菜单详解——理解android中的Menu--不错
原文网址:http://www.cnblogs.com/qingblog/archive/2012/06/08/2541709.html 前言 今天看了pro android 3中menu这一章,对A ...
- 如何理解Android中的xmlns
作为一名 Android 开发,我想大家对xmlns并不会陌生,因为在写布局文件(如下代码所示)的时候经常会碰到,虽然很多人对其含义并不是特别了解(比如说我).好吧,今天我们就来挖一挖这神奇的xmln ...
- Android 手写Binder 教你理解android中的进程间通信
关于Binder,我就不解释的太多了,网上一搜资料一堆,但是估计还是很多人理解的有困难.今天就教你如何从 app层面来理解好Binder. 其实就从我们普通app开发者的角度来看,仅仅对于androi ...
- Android菜单详解——理解android中的Menu
前言 今天看了pro android 3中menu这一章,对Android的整个menu体系有了进一步的了解,故整理下笔记与大家分享. PS:强烈推荐<Pro Android 3>,是我至 ...
- 深入理解Android中View
文章目录 [隐藏] 一.View是什么? 二.View创建的一个概述: 三.View的标志(Flag)系统 四.MeasureSpec 五.几个重要方法简介 5.1 onFinishInflate() ...
- 彻底理解 Android 中的阴影
如果我们想创造更好的 Android App,我相信我们需要遵循 Material Design 的设计规范.一般而言,Material Design 是一个包含光线,材质和投影的三维环境.如果我们想 ...
- 彻底理解 Android 中的阴影 1
如果我们想创造更好的 Android App,我相信我们需要遵循 Material Design 的设计规范.一般而言,Material Design 是一个包含光线,材质和投影的三维环境.如果我们想 ...
最新文章
- php redis删除所有key,php redis批量删除key的方法
- VS2017无法打开文件MSVCRTD.lib
- android同步aar到jcenter,android上传aar到jcenter
- python小工具(一)
- linux环境 jenkins +allure生成测试报告
- 论高校计算机信息管理能力的提升,论高校计算机信息管理能力的提升
- 同济保研计算机,同济大学保研率28%,保研高校前四:复旦、北大、交大、清华...
- 绘制ER图:PowerDesigner数据库设计软件讲解
- 网维大师icafe网吧管理软件 v6.0.3.3 官方
- 视频直播系统源码,简单的移动端轮播图
- Sublime 快捷键整理
- CMYK与RGB颜色区别
- 如何去掉firefox启动就是360主页 小白实操记录
- 第三章 分类模型-随机森林知识点详细总结
- postman支持https、安卓抓包
- AMD CPU 电脑突然画面声音突然卡顿,卡碟声,画面撕裂
- Redis—击穿、穿透、雪崩
- 【新闻早报简报】早上微信里发的那些新闻早报哪里来的
- Android 自定义View 实例2_Clipping Canvas
- go语言csv包_golang导出csv