写个自定义控件时经常要自定义一些自己的属性,平时用的都是那几个,今天就顺便一起总结一下这个东东吧~

  一、定义:属性的定义都在attrs.xml文件里面;

  二、读取:通过都是通过TypedArray去读取的,要获取TypedArray都是通过context.obtainStyledAttributes去获取的,它有几个重载方法,一般形如: TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomView);

  三、使用:要使用自定义属性,得先在布局文件声明 xmlns:app="http://schemas.android.com/apk/res-auto" 当然,你不喜欢app也可以自定义名字,形如:xmlns:custom="http://schemas.android.com/apk/res/{packagename}" 

  四、自定义format的概览:

format名称 format类型
reference
表示引用,参考某一资源ID
string
表示字符串
color
表示颜色值
boolean
表示尺寸值
dimension
表示布尔值
float
表示浮点值
integer
表示整型值
fraction
表示百分数
enum
表示枚举值
flag
表示位运算

  五、具体说明:

  5.1. reference:参考某一资源ID。

(1)属性定义:

<declare-styleable name = "名称">

<attr name = "cutom_id" format = "reference" />

</declare-styleable>

(2)属性使用:

<CustomView

android:layout_width = "wrap_content"
                     android:layout_height = "wrap_content"
                     app:cutom_id = "@drawable/图片ID"

/>

  5.2. color:颜色值。

(1)属性定义:

<declare-styleable name = "名称">

<attr name = "custom_color" format = "color" />

</declare-styleable>

(2)属性使用:

<CustomView

android:layout_width = "wrap_content"
                     android:layout_height = "wrap_content"
                     app:custom_color = "#00FF00"

/>

  5.3. boolean:布尔值。

(1)属性定义:

<declare-styleable name = "名称">

<attr name = "custom_b" format = "boolean" />

</declare-styleable>

(2)属性使用:

<CustomView

android:layout_width = "wrap_content"
                    android:layout_height = "wrap_content"

app:custom_b = "true"

/>

  5.4. dimension:尺寸值。

(1)属性定义:

<declare-styleable name = "名称">

<attr name = "custom_width" format = "dimension" />

</declare-styleable>

(2)属性使用:

<CustomView

android:layout_width = "wrap_content"
                    android:layout_height = "wrap_content"

       app:custom_width="44dp"

/>

  5.5. float:浮点值。

(1)属性定义:

<declare-styleable name = "名称">

<attr name = "custom_alpha" format = "float" />

</declare-styleable>

(2)属性使用:

<CustomView
              android:layout_width = "wrap_content"

 android:layout_height = "wrap_content"

       app:custom_alpha="0.5"

/>

  5.6. integer:整型值。

(1)属性定义:

<declare-styleable name = "名称">

<attr name = "custom_number" format="integer" />

</declare-styleable>

(2)属性使用:

<CustomView

       android:layout_width = "wrap_content"

  android:layout_height = "wrap_content"

       app:custom_number="5"

/>

  5.7. string:字符串。

(1)属性定义:

<declare-styleable name = "名称">
                   <attr name = "custom_key" format = "string" />
            </declare-styleable>

(2)属性使用:

<CustomView
                    android:layout_width = "wrap_content"
                    android:layout_height = "wrap_content"
                    app:custom_key = "test_msg"

/>

  5.8. fraction:百分数。

(1)属性定义:

<declare-styleable name="名称">
                   <attr name = "custom_percent" format = "fraction" />
            </declare-styleable>

(2)属性使用:

<CustomView

android:layout_width = "wrap_content"
                 android:layout_height = "wrap_content"

app:custom_percent = "200%"

/>

  5.9. enum:枚举值。

(1)属性定义:

<declare-styleable name="名称">
                   <attr name="custom_orientation">
                          <enum name="horizontal" value="0" />
                          <enum name="vertical" value="1" />
                   </attr>

</declare-styleable>

(2)属性使用:

<CustomView

android:layout_width = "wrap_content"
                    android:layout_height = "wrap_content"

app:custom_orientation = "vertical"
            />

  5.10. flag:位或运算。

(1)属性定义:

<declare-styleable name="名称">
                    <attr name="custom_mode">
                            <flag name = "mode_one" value = "0" />
                            <flag name = "mode_two" value = "1" />
                            <flag name = "mode_three" value = "2" />
                     </attr>

</declare-styleable>

(2)属性使用:

<CustomView

android:layout_width = "wrap_content"
                    android:layout_height = "wrap_content"

app:custom_mode = "mode_one|mode_two|mode_three"
            />

  5.11 注意: 属性定义时可以指定多种类型值。

    (1)属性定义:

<declare-styleable name = "名称">

<attr name = "custom_background" format = "reference|color" />

</declare-styleable>

(2)属性使用:

<CustomView

android:layout_width = "wrap_content"
                     android:layout_height = "wrap_content"
                     app:custom_background = "@drawable/图片ID|#00FF00"

/>

关于Android attrs 自定义属性的说明的更多相关文章

  1. Android中自定义属性(attrs.xml,TypedArray的使用)

    做Android布局是件很享受的事,这得益于他良好的xml方式.使用xml可以快速有效的为软件定义界面.可是有时候我们总感觉官方定义的一些基本组件不够用,自定义组件就不可避免了.那么如何才能做到像官方 ...

  2. android开发:Android 中自定义属性(attr.xml,TypedArray)的使用

    今天我们的教程是根据前面一节扩展进行的,如果你没有看,请点击 Android高手进阶教程(三)查看第三课,这样跟容易方便你的理解! 在xml 文件里定义控件的属性,我们已经习惯了android:att ...

  3. Android之自定义属性

    有些时候会觉得Android中提供的控件不能满足项目的要求,所以就会常常去自定义控件.自定义控件就不免会自定义属性.自定义属性大致需要三个步骤:在XML文件中定义自定义属性的名称和数据类型.在布局中调 ...

  4. Android中自定义属性的使用

    做Android布局是件很享受的事,这得益于他良好的xml方式.使用xml可以快速有效的为软件定义界面.可是有时候我们总感觉官方定义的一些基本组件不够用,自定义组件就不可避免了.那么如何才能做到像官方 ...

  5. Android attrs.xml文件中属性类型format值的格式

    "reference" //引用 "color" //颜色 "boolean" //布尔值 "dimension" // ...

  6. Android中自定义属性attr.xml的格式详解

    1. reference:参考某一资源ID.     (1)属性定义:             <declare-styleable name = "名称">      ...

  7. Android 自定义属性(attrs.xml,TypedArray)

    做Android布局是件很享受的事,这得益于他良好的xml方式.使用xml可以快速有效的为软件定义界面.可是有时候我们总感觉官方定义的一些基本组 件不够用,自定义组件就不可避免了.那么如何才能做到像官 ...

  8. Android学习笔记_49_Android中自定义属性(attrs.xml,TypedArray的使用)

    做Android布局是件很享受的事,这得益于他良好的xml方式.使用xml可以快速有效的为软件定义界面.可是有时候我们总感觉官方定义的一些基本组件不够用,自定义组件就不可避免了.那么如何才能做到像官方 ...

  9. [置顶] xamarin android自定义标题栏(自定义属性、回调事件)

    自定义控件的基本要求 这篇文章就当是自定义控件入门,看了几篇android关于自定义控件的文章,了解了一下,android自定义控件主要有3种方式: 自绘控件:继承View类,所展示的内容在OnDra ...

随机推荐

  1. CentOS利用Nginx+Docker部署.netcore应用

    安装docker 官方文档https://docs.docker.com/engine/installation/linux/docker-ce/centos/ [root@sn ~]# yum re ...

  2. Python自动化--语言基础6--模块操作之re、MySQL、Excel

    1.Python自有模块正则 import re # re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None print(re.match("aaa ...

  3. 消息队列(MQ)入门-activemq,代码级别

    第一种:activemq: 1.从官网下载apache-activemq-5.15.3-bin.zip并解压: 2.启动activemq, CMD--/bin/activemq start ,访问12 ...

  4. 用Open SSH生成公钥和私钥(Win)

    也可以使用 dsa 加密算法进行加密,命令如下: ssh-keygen -t dsa

  5. Egret学习笔记 (Egret打飞机-9.子弹对敌机和主角的碰撞)

    运行起来,虽然主角飞机和敌机都在互相发射子弹,但是子弹打中了就和没打中效果是一样的.. 这一章我们就来处理子弹和飞机的碰撞问题. 我们所有的操作都是基于Main这个容器来做的.所以我就把这个处理放到M ...

  6. BloomFilter(布隆过滤器)

    原文链接:http://blog.csdn.net/qq_38646470/article/details/79431659 1.概念: 如果想判断一个元素是不是在一个集合里,一般想到的是将所有元素保 ...

  7. CodeForces-740B Alyona and flowers

    题目要求选择一些花的集合,如果暴力去枚举每种选择方法明显是不行的.换种方式考虑:每一个集合都能为最后的答案做出要么正的.要么负的.要么0贡献,先把所有集合能做出的贡献预处理,然后从m个集合里面选择贡献 ...

  8. LOJ116 - 有源汇有上下界最大流

    原题链接 Description 模板题啦~ Code //有源汇有上下界最大流 #include <cstdio> #include <cstring> #include & ...

  9. Wireshark抓包常见出现错误

    转自这里 1.   tcp out-of-order(tcp有问题) 解答: 1).    应该有很多原因.但是多半是网络拥塞,导致顺序包抵达时间不同,延时太长,或者包丢失,需要重新组合数据单元 因为 ...

  10. FusionCharts MSBar2D图

    1.页面展示 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> ...