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

  一、定义:属性的定义都在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. img alt与title的区别

    前端 alt是图片加载不出来时候,对图片的文本替代 title 是鼠标放在图片上时,对图片的进一步说明 seo 搜索引擎对图片意思的理解主要靠 alt

  2. CENTOS6.6下mysql5.7.11的percona-xtrabackup安装与备份

    本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn Xtrabackup有两个主要的工具:xtrabackup.inno ...

  3. [翻译] 编写高性能 .NET 代码--第二章 GC -- 避免使用终结器,避免大对象,避免复制缓冲区

    避免使用终结器 如果没有必要,是不需要实现一个终结器(Finalizer).终结器的代码主要是让GC回收非托管资源用.它会在GC完成标记对象为可回收后,放入一个终结器队列里,在由另外一个线程执行队列里 ...

  4. [翻译] 编写高性能 .NET 代码--第二章 GC -- 将长生命周期对象和大对象池化

    将长生命周期对象和大对象池化 请记住最开始说的原则:对象要么立即回收要么一直存在.它们要么在0代被回收,要么在2代里一直存在.有些对象本质是静态的,生命周期从它们被创建开始,到程序停止才会结束.其它对 ...

  5. 关于eclipse的mysql连接配置

    打开eclipse,选择Window→Open Perspective→Other→Database Development,然后在右边的导航栏右键点击Database Connections文件夹, ...

  6. Windows API Finishing

    input { font-size: 14px; height: 26px } td { border-style: none; border-color: inherit; border-width ...

  7. 老男孩Python全栈开发(92天全)视频教程 自学笔记20

    day20课程内容: 模块: #模块一共三种:1.Python标准库 2.第三方模块 3.应用程序自定义的模块#import sysimport jisuanapp#print(jisuanapp.a ...

  8. DAY1--JAVA

    学习路线 2017-1-27打卡学习,先学习第一部分Java基础. 面向对象 面向对象---它是从现实世界中客观存在的事物(即对象)出发,并在系统构造中尽可能的运用人类自然的思维方式,来构建软件系统. ...

  9. 剑指offer随练

    合并两个排序的链表 题目描述 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. 思路:使用递归的方法,合并头节点,然后对剩下的链表接着合并头节点,直到合并完 ...

  10. HDU - 1584 IDA*

    思路:裸的IDA*,估计当前状态至少需要多少距离才能达到目标状态,剪枝即可.每一墩牌只需记录其最上面和最下面的牌型即可完成移动. AC代码 #include <cstdio> #inclu ...