精通Android自定义View(四)自定义属性使用详解
1、简述
对于自定义属性,遵循以下几步,就可以实现:
- 自定义一个CustomView(extends View )类
- 编写values/attrs.xml,在其中编写styleable和item等标签元素
- 在布局文件中CustomView使用自定义的属性(注意namespace)
- 在CustomView的构造方法中通过TypedArray获取
2、自定义一个View
2.1 定义View
public class CustomView extends View {//构造函数会在new的时候调用public CustomView(Context context) {this(context, null);}//在布局中使用public CustomView(Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);}//布局layout中调用,但是会有stylepublic CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}}
2.2 编写values/attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="CustomView"><attr name="text" format="string" /><attr name="test2" format="integer" /></declare-styleable></resources>
2.3 在布局文件中CustomView使用自定义的属性
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><com.studyyoun.customviewapplication.CustomViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"app:text="1234" /></LinearLayout>
2.4 在CustomView的构造方法中通过TypedArray获取
//在布局中使用public CustomView(Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);//获取自定义View中自定义属性的值TypedArray lTypedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomView);String text = lTypedArray.getString(R.styleable.CustomView_text);}
3、AttributeSet与TypedArray
- AttributeSet可以获得布局文件中定义的所有属性的key和value
//获取属性的数量int count = attrs.getAttributeCount();for (int i = 0; i < count; i++) {//属性名称String attrName = attrs.getAttributeName(i);//属性值String attrVal = attrs.getAttributeValue(i);Log.e("CustomView", "attrName = " + attrName + " , attrVal = " + attrVal);}
- TypedArray其实是用来简化我们的工作的,比如上例,如果布局中的属性的值是引用类型(比如:@dimen/dp100),如果使用AttributeSet去获得最终的像素值,那么需要第一步拿到id,第二步再去解析id。而TypedArray正是帮我们简化了这个过程
4、declare-styleable标签
- declare-styleable标签 中的name属性定义的是这组自定义属性组的名称,也就是说在自定义控件中引用自定义属性时所有用的标识符
如这里定义的 为 CustomView
<declare-styleable name="CustomView">... ...
</declare-styleable>在引用的时候为 TypedArray lTypedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomView);
5、定义可被多个自定义控件使用的属性
将属性定义在标签之外,这样自定义属性就可以被多个自定义控件使用了
<?xml version="1.0" encoding="utf-8"?>
<resources><!-- 将公用的属性定义在<declare-styleable></declare-styleable>标签之外,即可被多个自定义控件使用 --><attr name="pubbasecolor" format="color" /><declare-styleable name="test"><attr name="text" format="string" /><attr name="testAttr" format="integer" /></declare-styleable></resources>
精通Android自定义View(四)自定义属性使用详解相关推荐
- 精通Android自定义View(五)自定义属性值使用详情
1 可查看Android自定义View的基本使用 1 精通Android自定义View(一)自定义控的基本使用 2 精通Android自定义View(二)自定义属性使用详解 2 string 字符串 ...
- 【5年Android从零复盘系列之二十】Android自定义View(15):Matrix详解(图文)【转载】
[转载]本文转载自麻花儿wt 的文章<android matrix 最全方法详解与进阶(完整篇)> [5年Android从零复盘系列之二十]Android自定义View(15):Matri ...
- 【5年Android从零复盘系列之六】Android自定义View(1):基础详解(图文)
1.基础一:坐标计算 1.1 Android窗口坐标系计算以屏幕左上角为原点, 向右为X轴正向,向下为Y轴正向 1.2 View坐标系 [注意获取的坐标是像素值,不是dp值] [注意获取的坐标是像素值 ...
- Android 自定义View 之 RectF用法详解
在之前通过Circle画了一个奥运五环,这次通过RectF来画矩形,常规的就是长方形正方形之类的. 还是新建一个自定义View,CustomViewRectF,然后继承View,实现里面的两个基本的构 ...
- 精通Android自定义View(十四)绘制水平向右加载的进度条
1引言 1 精通Android自定义View(一)View的绘制流程简述 2 精通Android自定义View(二)View绘制三部曲 3 精通Android自定义View(三)View绘制三部曲综合 ...
- 精通Android自定义View(十二)绘制圆形进度条
1 绘图基础简析 1 精通Android自定义View(一)View的绘制流程简述 2 精通Android自定义View(二)View绘制三部曲 3 精通Android自定义View(三)View绘制 ...
- 精通Android自定义View(十九)自定义圆形炫彩加载转圈效果
1 效果 2 源码 public class JiondongView extends View {private Paint mBackgroundPaint;private float mScal ...
- 精通Android自定义View(十六)invalidate方法和requestLayout方法
1 简述 requestLayout方法会导致View的onMeasure.onLayout.onDraw方法被调用:invalidate方法则只会导致View的onDraw方法被调用 2 reque ...
- 精通Android自定义View(十三)事件分发简述
1 事件序列 (1)手指接触屏幕后会产生一系列事件,事件分为3种:ACTION_DOWN(手指刚刚接触屏幕).ACTION_MOVE(手指在屏幕移动).ACTION_UP(手指从屏幕松开) (2)一个 ...
最新文章
- 用实例给新手讲解RSA加密算法
- VMware Fusion DHCP方式下如何指定虚拟机IP地址
- 一个漂亮的输出MySql数据库表结构的PHP页面
- (三十)java多线程一
- linux 线程学习初步01
- imx6 android快速启动,freescale imx6 开机启动速度优化之Bootchart工具的使用问题
- Redis主从配置详细过程
- CF 246E. Blood Cousins Return [dsu on tree STL]
- java .class的作用_Java中Class类的作用与深入理解
- kotlin语言学习文档 for Android
- URLEncoder和URLDecoder;cookie中保存特殊字符以及URL中乱码问题解决方案
- PyCharm汉化包安装下载及PyCharm无法进行设置问题
- mysql数据库如何授权_MySQL数据库学习笔记 ---- 数据库授权
- java excel转pdf linux_docker安装libreoffice并实现把Excel转为pdf
- linux 极路由救砖,极路由三不死u-boot刷机方法
- 【渝粤题库】国家开放大学2021春1062文学英语赏析题目
- codeforces 558D Guess Your Way Out! II 规律
- 《经营的本质》1-“经营的基本元素”读后感及读书笔记
- kon-boot启动盘测试
- 薪火相传 点亮企业数智化舞台—— CDEC2021中国数字智能生态大会西安收官
热门文章
- mtensor一个tensor计算库,支持cuda延迟计算
- Science报道新研究:同行评审后的研究仅比预印本研究质量提高4%
- GitHub标星6000+!Python带你实践机器学习圣经PRML
- YouTube上最火的OpenCV-Python入门视频教程
- nonlocal python3_Python 中的 global、nonlocal 辨析
- 自学Python能干些什么副业
- 移动端实时3D目标检测,谷歌开源出品,安卓下载就能用
- NLP系列学习:CRF条件随机场(1)
- 机器学习面试之偏差方差
- 照片尺寸大小对照表_贴片led灯珠型号对照表在手,贴片灯珠选型从此不愁!