Android自定义控件增加xml标签属性、取值等
Android中常常用到写自己的控件来满足自己的开发需求,自定义控件在布局中使用的时候,如何增加标签属性来配置控件属性,又如何在控件中使用自己添加的属性
一、在资源文件中配置标签属性
在资源文件res/values/attrs.xml中增加 declare-styleable 节点,name为自定义控件名字;如下
<resources><declare-styleable name="CustomerView">//项目中资源id<attr name="background" format="reference" /><attr name="src" format="reference" />//颜色<attr name = "textColor" format = "color" /> //布尔值<attr name = "focusable" format = "boolean" /> //尺寸值 dp,sp,px等<attr name = "width" format = "dimension" />//字符串 <attr name = "textStr" format = "string" /> //枚举值<attr name="orientation"> <enum name="horizontal" value="0" /> <enum name="vertical" value="1" /> </attr> </declare-styleable>
</resources>
其中子节点attr中的name值backound、src、textColor、focusable、width、textStr均为范例,自己写的时候自定义属性名称
二、xml布局文件的控件中引用自己定义的属性
1、引入app的命名空间,自定义属性以"app:"为前缀
<layout xmlns:app="http://schemas.android.com/apk/res-auto"><com.example.weiget.CustomerViewapp:textStr="@string/app_name"app:background="@string/app_name"android:src="@drawable/compile"android:layout_width="match_parent"android:layout_height="match_parent"/>
</layout>
三、项目中自定义属性和布局文件中的属性值都配置好了,如何在自定义控件文件中拿到配置的参数呢;如
public class CustomerView extends AppCompatImageView {public CustomerView(Context context) {super(context);init(context,null);}public CustomerView(Context context, AttributeSet attrs) {super(context, attrs);init(context,attrs);}public CustomerView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init(context,attrs);}private void init(Context context, AttributeSet attrs) {if(attrs != null) {//从项目style中文件中取出样式数组TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomerView);//取到xml布局文件中配置的资源文件Drawable drawable = typedArray.getDrawable(R.styleable.CustomerView_src);//字符串String string = typedArray.getString(R.styleable.CustomerView_textStr);//布尔值boolean aBoolean = typedArray.getBoolean(R.styleable.CustomerView_focusable, false);}}
}
从typeArray数组中取值的参数,其中参数命门规则为R.styleable.控件名_属性名
Android自定义控件增加xml标签属性、取值等相关推荐
- MyBatis→SqlSession、sqlMapConfig.xml、映射XML文件、OGNL、拼接SQL标签、取值查值、批量SQL、一对多多对一多对多
官网 https://mybatis.org/mybatis-3/ SqlSession sqlMapConfig.xml OGNL XML拼接SQL标签 参数取值 预编译与直接赋值 编码规范 sel ...
- CSS clear 属性取值详解
之前有整理过一部分知识点, 一直没有发布, 因为都是有关 前端 方面的零散内容; 现在想想无论分享什么内容都需要慢慢积累, 所以还是决定将之前整理的相关内容验证之后慢慢分享给大家 这个专题 就是 工作 ...
- FusionCharts 的XML标签属性
FusionCharts 的 XML标签属性有一下四种数据类型 * Boolean - 布尔类型,只能为1或者0.例如:<graph showNames='1′ > * Number - ...
- php按条件修改xml,php 修改、增加xml结点属性的实现代码
php修改xml结点属性,增加xml结点属性的代码,有需要的朋友可以参考下 php 修改 增加xml结点属性的代码,供大家学习参考. php修改xml结点属性,增加xml结点属性的代码,有需要的朋友, ...
- jsp标签自定义属性取值问题
问题:jsp标签自定义属性取值问题 正确方法:var testValue = obj.getAttribute("testValue"); var testValue = obj. ...
- JSP自定义select标签中取值
JSP自定义select标签的取值 select取值是通过name属性获取. 自定义标签的流程: 1.继承JSP标签的标签类TagSupport,SimpleTagSupport等,重写dostart ...
- Python:高级主题之(属性取值和赋值过程、属性描述符、装饰器)
属性取值和赋值过程 一切皆是对象,类型也是对象. 对象包含一个__class__属性指向其所属类型. 对象包含一个__dict__属性指向其所包含的成员(属性和方法). 取值过程(下面是伪代码) 1 ...
- span标签的取值与赋值
1.span标签的取值 <!--js的取值方式--> <span id="span_id">span的文本</span> <script& ...
- python读取文件并按属性取值
python python读取文件并按属性取值 python读取文件并按属性取值 定义类,使得传入的值后续可以通过属性取出 class sepByAttr: # 自定义的元素def __init__( ...
最新文章
- instanceof与typeof 运算符
- HBA and WWN
- 关于redis的几件小事(一)redis的使用目的与问题
- 别再说程序员不懂时尚
- php和python哪个工资高-python工资高还是java?
- lvs服务器需要开启web服务么_Centos7搭建LVS+Keepalived高可用Web
- proteus仿真micropython_基于micropython的滑动平均滤波器
- javamail发送邮件,解决被垃圾邮件问题
- SpringBoot入门篇-简介
- 小米球Ngrok-使用方法
- 你是否有过出现图片有色差色偏的问题(python用matplotlib显示opencv绘制的图像)
- 用verilog实现一个 一位全加器
- armv6、armv7、armv7s、armv8、armv64及其i386、x86_64区别
- wps office 2019中文版
- IT学生解惑真经(转) (真的好经典!)
- InTouch蜂鸣器报警提示方法
- 数字身份的万亿市场之争才刚开始
- matlab多项式计算题目,MATLAB数据分析与多项式计算-习题答案
- 8.10 12.1-12.5
- 常见的考勤管理系统有哪些功能?