前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >android attrs获取_关于Android attrs 自定义属性的说明

android attrs获取_关于Android attrs 自定义属性的说明

作者头像
全栈程序员站长
发布2022-11-02 11:01:24
9290
发布2022-11-02 11:01:24
举报
文章被收录于专栏:全栈程序员必看

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

一、定义:属性的定义都在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)属性定义:

(2)属性使用:

android:layout_width = “wrap_content”

android:layout_height = “wrap_content”

app:cutom_id = “@drawable/图片ID”

/>

5.2. color:颜色值。

(1)属性定义:

(2)属性使用:

android:layout_width = “wrap_content”

android:layout_height = “wrap_content”

app:custom_color = “#00FF00”

/>

5.3. boolean:布尔值。

(1)属性定义:

(2)属性使用:

android:layout_width = “wrap_content”

android:layout_height = “wrap_content”

app:custom_b = “true”

/>

5.4. dimension:尺寸值。

(1)属性定义:

(2)属性使用:

android:layout_width = “wrap_content”

android:layout_height = “wrap_content”

app:custom_width=”44dp”

/>

5.5. float:浮点值。

(1)属性定义:

(2)属性使用:

android:layout_width = “wrap_content”

android:layout_height = “wrap_content”

app:custom_alpha=”0.5″

/>

5.6. integer:整型值。

(1)属性定义:

(2)属性使用:

android:layout_width = “wrap_content”

android:layout_height = “wrap_content”

app:custom_number=”5″

/>

5.7. string:字符串。

(1)属性定义:

(2)属性使用:

android:layout_width = “wrap_content”

android:layout_height = “wrap_content”

app:custom_key = “test_msg”

/>

5.8. fraction:百分数。

(1)属性定义:

(2)属性使用:

android:layout_width = “wrap_content”

android:layout_height = “wrap_content”

app:custom_percent = “200%”

/>

5.9. enum:枚举值。

(1)属性定义:

(2)属性使用:

android:layout_width = “wrap_content”

android:layout_height = “wrap_content”

app:custom_orientation = “vertical”

/>

5.10. flag:位或运算。

(1)属性定义:

(2)属性使用:

android:layout_width = “wrap_content”

android:layout_height = “wrap_content”

app:custom_mode = “mode_one|mode_two|mode_three”

/>

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

(1)属性定义:

(2)属性使用:

android:layout_width = “wrap_content”

android:layout_height = “wrap_content”

app:custom_background = “@drawable/图片ID|#00FF00”

/>

原文:http://www.cnblogs.com/wytings/p/6974217.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/180939.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年10月17日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档