关于Android attrs 自定义属性的说明
写个自定义控件时经常要自定义一些自己的属性,平时用的都是那几个,今天就顺便一起总结一下这个东东吧~
一、定义:属性的定义都在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 |
|
| 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 自定义属性的说明的更多相关文章
- Android中自定义属性(attrs.xml,TypedArray的使用)
做Android布局是件很享受的事,这得益于他良好的xml方式.使用xml可以快速有效的为软件定义界面.可是有时候我们总感觉官方定义的一些基本组件不够用,自定义组件就不可避免了.那么如何才能做到像官方 ...
- android开发:Android 中自定义属性(attr.xml,TypedArray)的使用
今天我们的教程是根据前面一节扩展进行的,如果你没有看,请点击 Android高手进阶教程(三)查看第三课,这样跟容易方便你的理解! 在xml 文件里定义控件的属性,我们已经习惯了android:att ...
- Android之自定义属性
有些时候会觉得Android中提供的控件不能满足项目的要求,所以就会常常去自定义控件.自定义控件就不免会自定义属性.自定义属性大致需要三个步骤:在XML文件中定义自定义属性的名称和数据类型.在布局中调 ...
- Android中自定义属性的使用
做Android布局是件很享受的事,这得益于他良好的xml方式.使用xml可以快速有效的为软件定义界面.可是有时候我们总感觉官方定义的一些基本组件不够用,自定义组件就不可避免了.那么如何才能做到像官方 ...
- Android attrs.xml文件中属性类型format值的格式
"reference" //引用 "color" //颜色 "boolean" //布尔值 "dimension" // ...
- Android中自定义属性attr.xml的格式详解
1. reference:参考某一资源ID. (1)属性定义: <declare-styleable name = "名称"> ...
- Android 自定义属性(attrs.xml,TypedArray)
做Android布局是件很享受的事,这得益于他良好的xml方式.使用xml可以快速有效的为软件定义界面.可是有时候我们总感觉官方定义的一些基本组 件不够用,自定义组件就不可避免了.那么如何才能做到像官 ...
- Android学习笔记_49_Android中自定义属性(attrs.xml,TypedArray的使用)
做Android布局是件很享受的事,这得益于他良好的xml方式.使用xml可以快速有效的为软件定义界面.可是有时候我们总感觉官方定义的一些基本组件不够用,自定义组件就不可避免了.那么如何才能做到像官方 ...
- [置顶]
xamarin android自定义标题栏(自定义属性、回调事件)
自定义控件的基本要求 这篇文章就当是自定义控件入门,看了几篇android关于自定义控件的文章,了解了一下,android自定义控件主要有3种方式: 自绘控件:继承View类,所展示的内容在OnDra ...
随机推荐
- 读取Pdm文件内容(含源码)
Pdm文件,就是PowerDesigner软件生成的文件,用来设计数据库表结构非常适合.其实,它的文件存储格式就是Xml,网上有很多代码,可以读取pdm文件内容.代码可以使用,但一般只能读取简单的pd ...
- Java经典编程题50道之四十五
判断一个整数能被几个9整除. public class Example45 { public static void main(String[] args) { f(729); ...
- haproxy配置文件详解和ACL功能
*/ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...
- SpringBoot中关于Mybatis使用的三个问题
SpringBoot中关于Mybatis使用的三个问题 转载请注明源地址:http://www.cnblogs.com/funnyzpc/p/8495453.html 原本是要讲讲PostgreSQL ...
- java网络编程(1)
太久没有用java做一些东西了,搞太多的协议框架,基本的东西好像快忘记了~每天抽出一点时间出来,来好好温习下基础,顺便记录下来,以后还忘记可以回来看看==.首先从网络编程开始吧==.这玩意太久没有用了 ...
- 【Learning】 莫比乌斯反演
莫比乌斯反演 对于两个定义域为非负整数的函数\(F(n)\)和\(f(n)\) 若满足:\(F(n)=\sum\limits_{d|n}f(d)\),则反演得到\(f(n)=\sum\limi ...
- JMeter使用(Linux)
JMeter是一个Java桌面应用程序,用户界面采用Swing Java Api实现,支持并发和多线程或者线程组的执行,对于配置负载测试和压力测试非常有用.Jmeter是开源.免安装的,只需要有jdk ...
- 转 Caffe学习系列(9):运行caffe自带的两个简单例子
为了程序的简洁,在caffe中是不带练习数据的,因此需要自己去下载.但在caffe根目录下的data文件夹里,作者已经为我们编写好了下载数据的脚本文件,我们只需要联网,运行这些脚本文件就行了. 注意: ...
- Linux显示进程状态
Linux显示进程状态 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ top top - 19:16:36 up 45 min, 2 users, load ...
- TortoiseSVN设置忽略文件和目录文件夹
TortoiseSVN设置忽略文件和目录文件夹 在多数项目中你总会有文件和目录不需要进行版本控制.这可能包括一些由编译器生成的文件,*.obj,*.lst,或许是一个用于存放可执行程序的输出文件夹. ...