declare-styleable:declare-styleable是给自定义控件添加自定义属性用的。

1.首先,先写attrs.xml

在res-vlaues文件夹下创建资源文件attrs.xml或则自定义一个资源文件xx.xml,都可以。

之后在里面配置declare-styleable ,name为PersonAttr

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <declare-styleable name="PersonAttr">
  4. <attr name="name" format="reference" />
  5. <attr name="sex" format="reference" />
  6. <attr name="age" format="integer" />
  7. <attr name="weight">
  8. <flag name="fat" value="2" />
  9. <flag name="mid" value="1" />
  10. <flag name="thin" value="0" />
  11. </attr>
  12. <attr name="adult" format="boolean" />
  13. <attr name="textSize" format="dimension" />
  14. </declare-styleable>
  15. </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",就是里面对应了自己的属性值。

11. reference|color:颜色的资源文件。

12.reference|boolean:布尔值的资源文件

注意://由于reference是从资源文件中获取:所以在XML文件中写这个属性的时候必须 personattr:name="@string/app_name"这种格式,否则会出错

2.设置好属性文件后,在使用的布局中写相关配置:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:personattr="http://schemas.android.com/apk/res/com.example.declare_styleable"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent" >
  5. <com.example.declare_styleable.PersonView
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"
  8. personattr:name="@string/person_name"
  9. personattr:weight ="fat"
  10. personattr:adult ="false"
  11. personattr:textSize="@dimen/text_size"/>
  12. </RelativeLayout>

这里要先应用这个attr:

  1. xmlns:personattr="http://schemas.android.com/apk/res/com.example.declare_styleable"

对应结构是:

  1. xmlns:你自己定义的名称="http://schemas.android.com/apk/res/你程序的package包名"    (我这是com.example.declare_styleable)

包名是配置文件中   package="com.example.declare_styleable" 这样格式的

之后在布局中自定义的类中设相关属性:

你自己定义的名称:你设的属性 ="属性值";

3.最后在自定义控件的构造方法中获取你配置的属性值:

  1. public class PersonView extends TextView {
  2. public PersonView(Context context) {
  3. super(context);
  4. // TODO Auto-generated constructor stub
  5. }
  6. public PersonView(Context context, AttributeSet attrs, int defStyle) {
  7. super(context, attrs, defStyle);
  8. // TODO Auto-generated constructor stub
  9. }
  10. public PersonView(Context context, AttributeSet attrs) {
  11. super(context, attrs);
  12. // TODO Auto-generated constructor stub
  13. TypedArray tArray = context.obtainStyledAttributes(attrs,R.styleable.PersonAttr);//获取配置属性
  14. String name = tArray.getString(R.styleable.PersonAttr_name);<span style="font-family: Arial, Helvetica, sans-serif;">//得到属性name</span>
  15. int age = tArray.getInt(R.styleable.PersonAttr_age, 15);
  16. Boolean adult = tArray.getBoolean(R.styleable.PersonAttr_adult, false);
  17. String str_adult = getAdultStatus(adult);
  18. int weight = tArray.getInt(R.styleable.PersonAttr_weight, 1);// 默认是中等身材,属性为:1
  19. String str_weight = getWeightStatus(weight);//获得肥胖属性
  20. float textSize = tArray.getDimension(R.styleable.PersonAttr_textSize,R.dimen.default_text_size);// 如果你设置为DP等单位,会做像素转换
  21. tArray.recycle();//回收资源
  22. //      setTextSize(textSize);//设置字体大小
  23. setText("姓名:" + name + "\n" + "年龄:" + age + "\n" + "是否成年:" + str_adult
  24. + "\n" + "体形:" + str_weight);//给自定义的控件赋值
  25. }
  26. /** 根据传入的值判断是否成年 */
  27. public String getAdultStatus(Boolean adult ){
  28. String str_adult = "未成年";
  29. if (adult) {
  30. str_adult = "成年";
  31. }
  32. return str_adult;
  33. }
  34. /** 根据传入的值判断肥胖状态 */
  35. public String getWeightStatus(int weight){
  36. String str_weight = "中等";
  37. switch (weight) {
  38. case 0:
  39. str_weight = "瘦";
  40. break;
  41. case 1:
  42. str_weight = "中等";
  43. break;
  44. case 2:
  45. str_weight = "肥胖";
  46. break;
  47. default:
  48. break;
  49. }
  50. return str_weight;
  51. }
  52. }

运行后就是:

declare-styleable的使用的更多相关文章

  1. Android 之 自定义标签 和 自定义组件

    1    自定义标签 这是我的模板项目目录     既然想像 android:text  那样使用自己的标签,那么首先得有标签. 在 res/values/ 下我新建了个 mm_tag.xml (切记 ...

  2. mysql substring函数截取值后赋给一个declare变量

    今天写的一个mysql存储过程涉及到对一个传入参数的字符串截取,然后需要判断截取字符串进行一系列操作,最开始用select  subtring() into 这样的方法将截取值赋于declare变量直 ...

  3. android r.styleable是什么或报错

    r.styleable 是自定义控件 自定义控件写好的后,需要在res-value-attrs.xml中定义,如: <declare-styleable name="SlidingMe ...

  4. 关于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\ 这个问题出现没 ...

  5. rabbitmq method之queue.declare

    queue.declare即申请队列,首先对队列名作处理,若未指定队列名则随机生成一个,然后查询数据库队列是否已经创建,若创建完成则会申请队列返回 handle_method(#'queue.decl ...

  6. LINUX:read、array、declare

    read:要读取来自键盘输入的变量 使用规则: read [-pt] variale 选项与参数: -p:后面接提示字符: -t:后面接等待的“秒数”: 如果read之后不加任何参数,直接加上变量名称 ...

  7. Ubuntu 下使用declare的问题

    Ubuntu在shell的执行上用户root和普通用户是不一样的. 使用vi /etc/passwd 我们就可以看到在用户的最后一行也就是定义shell执行位置的地方root的位置是/bin/bash ...

  8. dojo分析之declare接口

    欢迎转载opendevkit文章, 文章原始地址: http://www.opendevkit.com/?e=57 declare接口是dojo定义类系统的关键函数, 类系统就是抽象,封装,继承.do ...

  9. 关于The serializable class XXX does not declare a static final serialVersionUID field of type long的警告

    编写实体类并且继承序列化接口时候,实体类会有警告,要生成一个静态的serialVersionUID. 上网搜了一下资料,现通俗解释一下: 点击前2个选项,会生成: private static fin ...

  10. PHP declare(ticks=N); 的作用

    一般用法是 declare(ticks=N);拿declare(ticks=1)来说,这句主要作用有两种: 1.Zend引擎每执行1条低级语句就去执行一次 register_tick_function ...

随机推荐

  1. centos 修改ssh端口,以支持vsftp

    vi /etc/ssh/sshd_config Port 22 Port 2225执行/etc/init.d/sshd restart   启动SSH服务,这样SSH端口将同时工作与22和2225上. ...

  2. css中标签,类名,id名的命名 语义化命名

    作为前端开发人,经常头疼于雷鸣,标签,id名的命名,不知道应该基于什么原则. 原则: 2 当命名的时候,问自己,这个元素是要来做什么?(根据使用目的).ad-banner 4 避免依靠位置和视觉效果命 ...

  3. [你必须知道的.NET]目录导航

    http://www.cnblogs.com/anytao/archive/2007/09/14/must_net_catalog.html

  4. DeltaFish 校园物资共享平台 第三次小组会议

    一.想法 娄雨禛: 网页底层开发转移到后端,快速建站,效率高. 可以依照模板进行仿制. 可以考虑只进行页面设计. 但是出现问题不会调试. 所以自己写源码,做出一个大致的样子. 二.上周进度汇报 齐天杨 ...

  5. 分类(Category)的本质 及其与类扩展(Extension) /继承(Inherit)的区别

    1.分类的概念 分类是为了扩展系统类的方法而产生的一种方式,其作用就是在不修改原有类的基础上,为一个类扩展方法,最主要的是可以给系统类扩展我们自己定义的方法. 如何创建一个分类?↓↓ ()Cmd+N, ...

  6. Data mapping-数据映射

    数据映射:根据数据的结构信息建立数据间的映射操作机制. 数据映射的要素: 一.数据 1.源数据: 2.目标数据: 3.数据间关系: 4.数据的元数据(结构信息). 5.元素类型的对应关系. 二.元数据 ...

  7. day003 python解释器、变量和数据类型基础讲解

    Python解释器 打开官网https://www.python.org/downloads/windows/ 下载中心下载对应电脑版本的Python安装包,选择custom后一路next安装完成.过 ...

  8. PAT_A1111#Online Map

    Source: PAT A1111 Online Map (30 分) Description: Input our current position and a destination, an on ...

  9. Let's Encrypt,免费好用的 HTTPS 证书

    转自:   https://imququ.com/post/letsencrypt-certificate.html?hmsr=toutiao.io&utm_medium=toutiao.io ...

  10. [luogu4161 SCOI2009]游戏 (DP)

    传送门 Solution 可以发现实际上是把n分为几个循环节,然后找循环节的\(lcm\)是这次的排数 而\(lcm\)必然是一些最高次幂的质数的成积,那么就dp求一下所有情况就好了 PS:注意并不是 ...