declare-styleable的使用
declare-styleable:declare-styleable是给自定义控件添加自定义属性用的。
1.首先,先写attrs.xml
在res-vlaues文件夹下创建资源文件attrs.xml或则自定义一个资源文件xx.xml,都可以。
之后在里面配置declare-styleable ,name为PersonAttr
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <declare-styleable name="PersonAttr">
- <attr name="name" format="reference" />
- <attr name="sex" format="reference" />
- <attr name="age" format="integer" />
- <attr name="weight">
- <flag name="fat" value="2" />
- <flag name="mid" value="1" />
- <flag name="thin" value="0" />
- </attr>
- <attr name="adult" format="boolean" />
- <attr name="textSize" format="dimension" />
- </declare-styleable>
- </resources>
我这里设置了姓名name,性别sex,年龄age,以及特征属性weight(fat,mid,thin内部的3个属性及对应的属性值),还有是否成年adult,和TextView的字体大小textView。
可能这里有人会问,format是什么,里面的单词代表的又是什么意思。
format就是格式,里面的就是这个属性对应的格式,下面列出来大致的格式有:
1. reference:参考某一资源ID,以此类推
(1)属性定义:
<declare-styleable name = "名称">
<attr name = "background" format = "reference" />
</declare-styleable>
(2)属性使用:
<ImageView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/图片ID"
/>
2. color:颜色值
<declare-styleable name = "名称">
<attr name = "textColor" format = "color" />
</declare-styleable>
3. boolean:布尔值
<declare-styleable name = "名称">
<attr name = "focusable" format = "boolean" />
</declare-styleable>
4. dimension:尺寸值。注意,这里如果是dp那就会做像素转换
<declare-styleable name = "名称">
<attr name = "layout_width" format = "dimension" />
</declare-styleable>
5. float:浮点值。
6. integer:整型值。
7. string:字符串
8. fraction:百分数。
9. enum:枚举值
10. flag:是自己定义的,类似于 android:gravity="top",就是里面对应了自己的属性值。
12.reference|boolean:布尔值的资源文件
注意://由于reference是从资源文件中获取:所以在XML文件中写这个属性的时候必须 personattr:name="@string/app_name"这种格式,否则会出错
2.设置好属性文件后,在使用的布局中写相关配置:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:personattr="http://schemas.android.com/apk/res/com.example.declare_styleable"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <com.example.declare_styleable.PersonView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- personattr:name="@string/person_name"
- personattr:weight ="fat"
- personattr:adult ="false"
- personattr:textSize="@dimen/text_size"/>
- </RelativeLayout>
这里要先应用这个attr:
- xmlns:personattr="http://schemas.android.com/apk/res/com.example.declare_styleable"
对应结构是:
- xmlns:你自己定义的名称="http://schemas.android.com/apk/res/你程序的package包名" (我这是com.example.declare_styleable)
包名是配置文件中 package="com.example.declare_styleable" 这样格式的
之后在布局中自定义的类中设相关属性:
你自己定义的名称:你设的属性 ="属性值";
3.最后在自定义控件的构造方法中获取你配置的属性值:
- public class PersonView extends TextView {
- public PersonView(Context context) {
- super(context);
- // TODO Auto-generated constructor stub
- }
- public PersonView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- // TODO Auto-generated constructor stub
- }
- public PersonView(Context context, AttributeSet attrs) {
- super(context, attrs);
- // TODO Auto-generated constructor stub
- TypedArray tArray = context.obtainStyledAttributes(attrs,R.styleable.PersonAttr);//获取配置属性
- String name = tArray.getString(R.styleable.PersonAttr_name);<span style="font-family: Arial, Helvetica, sans-serif;">//得到属性name</span>
- int age = tArray.getInt(R.styleable.PersonAttr_age, 15);
- Boolean adult = tArray.getBoolean(R.styleable.PersonAttr_adult, false);
- String str_adult = getAdultStatus(adult);
- int weight = tArray.getInt(R.styleable.PersonAttr_weight, 1);// 默认是中等身材,属性为:1
- String str_weight = getWeightStatus(weight);//获得肥胖属性
- float textSize = tArray.getDimension(R.styleable.PersonAttr_textSize,R.dimen.default_text_size);// 如果你设置为DP等单位,会做像素转换
- tArray.recycle();//回收资源
- // setTextSize(textSize);//设置字体大小
- setText("姓名:" + name + "\n" + "年龄:" + age + "\n" + "是否成年:" + str_adult
- + "\n" + "体形:" + str_weight);//给自定义的控件赋值
- }
- /** 根据传入的值判断是否成年 */
- public String getAdultStatus(Boolean adult ){
- String str_adult = "未成年";
- if (adult) {
- str_adult = "成年";
- }
- return str_adult;
- }
- /** 根据传入的值判断肥胖状态 */
- public String getWeightStatus(int weight){
- String str_weight = "中等";
- switch (weight) {
- case 0:
- str_weight = "瘦";
- break;
- case 1:
- str_weight = "中等";
- break;
- case 2:
- str_weight = "肥胖";
- break;
- default:
- break;
- }
- return str_weight;
- }
- }
运行后就是:
declare-styleable的使用的更多相关文章
- Android 之 自定义标签 和 自定义组件
1 自定义标签 这是我的模板项目目录 既然想像 android:text 那样使用自己的标签,那么首先得有标签. 在 res/values/ 下我新建了个 mm_tag.xml (切记 ...
- mysql substring函数截取值后赋给一个declare变量
今天写的一个mysql存储过程涉及到对一个传入参数的字符串截取,然后需要判断截取字符串进行一系列操作,最开始用select subtring() into 这样的方法将截取值赋于declare变量直 ...
- android r.styleable是什么或报错
r.styleable 是自定义控件 自定义控件写好的后,需要在res-value-attrs.xml中定义,如: <declare-styleable name="SlidingMe ...
- 关于Django 错误 doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS
记录一下 报错 doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS\ 这个问题出现没 ...
- rabbitmq method之queue.declare
queue.declare即申请队列,首先对队列名作处理,若未指定队列名则随机生成一个,然后查询数据库队列是否已经创建,若创建完成则会申请队列返回 handle_method(#'queue.decl ...
- LINUX:read、array、declare
read:要读取来自键盘输入的变量 使用规则: read [-pt] variale 选项与参数: -p:后面接提示字符: -t:后面接等待的“秒数”: 如果read之后不加任何参数,直接加上变量名称 ...
- Ubuntu 下使用declare的问题
Ubuntu在shell的执行上用户root和普通用户是不一样的. 使用vi /etc/passwd 我们就可以看到在用户的最后一行也就是定义shell执行位置的地方root的位置是/bin/bash ...
- dojo分析之declare接口
欢迎转载opendevkit文章, 文章原始地址: http://www.opendevkit.com/?e=57 declare接口是dojo定义类系统的关键函数, 类系统就是抽象,封装,继承.do ...
- 关于The serializable class XXX does not declare a static final serialVersionUID field of type long的警告
编写实体类并且继承序列化接口时候,实体类会有警告,要生成一个静态的serialVersionUID. 上网搜了一下资料,现通俗解释一下: 点击前2个选项,会生成: private static fin ...
- PHP declare(ticks=N); 的作用
一般用法是 declare(ticks=N);拿declare(ticks=1)来说,这句主要作用有两种: 1.Zend引擎每执行1条低级语句就去执行一次 register_tick_function ...
随机推荐
- centos 修改ssh端口,以支持vsftp
vi /etc/ssh/sshd_config Port 22 Port 2225执行/etc/init.d/sshd restart 启动SSH服务,这样SSH端口将同时工作与22和2225上. ...
- css中标签,类名,id名的命名 语义化命名
作为前端开发人,经常头疼于雷鸣,标签,id名的命名,不知道应该基于什么原则. 原则: 2 当命名的时候,问自己,这个元素是要来做什么?(根据使用目的).ad-banner 4 避免依靠位置和视觉效果命 ...
- [你必须知道的.NET]目录导航
http://www.cnblogs.com/anytao/archive/2007/09/14/must_net_catalog.html
- DeltaFish 校园物资共享平台 第三次小组会议
一.想法 娄雨禛: 网页底层开发转移到后端,快速建站,效率高. 可以依照模板进行仿制. 可以考虑只进行页面设计. 但是出现问题不会调试. 所以自己写源码,做出一个大致的样子. 二.上周进度汇报 齐天杨 ...
- 分类(Category)的本质 及其与类扩展(Extension) /继承(Inherit)的区别
1.分类的概念 分类是为了扩展系统类的方法而产生的一种方式,其作用就是在不修改原有类的基础上,为一个类扩展方法,最主要的是可以给系统类扩展我们自己定义的方法. 如何创建一个分类?↓↓ ()Cmd+N, ...
- Data mapping-数据映射
数据映射:根据数据的结构信息建立数据间的映射操作机制. 数据映射的要素: 一.数据 1.源数据: 2.目标数据: 3.数据间关系: 4.数据的元数据(结构信息). 5.元素类型的对应关系. 二.元数据 ...
- day003 python解释器、变量和数据类型基础讲解
Python解释器 打开官网https://www.python.org/downloads/windows/ 下载中心下载对应电脑版本的Python安装包,选择custom后一路next安装完成.过 ...
- PAT_A1111#Online Map
Source: PAT A1111 Online Map (30 分) Description: Input our current position and a destination, an on ...
- Let's Encrypt,免费好用的 HTTPS 证书
转自: https://imququ.com/post/letsencrypt-certificate.html?hmsr=toutiao.io&utm_medium=toutiao.io ...
- [luogu4161 SCOI2009]游戏 (DP)
传送门 Solution 可以发现实际上是把n分为几个循环节,然后找循环节的\(lcm\)是这次的排数 而\(lcm\)必然是一些最高次幂的质数的成积,那么就dp求一下所有情况就好了 PS:注意并不是 ...