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

android attrs获取_关于Android attrs 自定义属性的说明写个自定义控件时经常要自定义一些自己的属性,平时用的都是那几个,今天就顺便一起总结一下这个东东吧~一、定义:属性的定义都在attrs.xml文件里面;二、读取:通过都是通过TypedArray去读取的,要获取TypedArray都是通过context.obtainStyledAttributes去获取的,它有几个重载方法,一般形如:TypedArraytypedArray=context….

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

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

一、定义:属性的定义都在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.net/180939.html原文链接:https://javaforall.net

(0)
上一篇 2022年10月17日 下午4:46
下一篇 2022年10月17日 下午5:00


相关推荐

  • linux find命令详解_linux中find命令用法

    linux find命令详解_linux中find命令用法find命令格式:findpath-option[-print][-exec-okcommand]{}\;find命令的参数:path:要查找的目录路径。~表示$HO

    2022年7月29日
    10
  • idea构建springboot_钉钉的使用方法和技巧

    idea构建springboot_钉钉的使用方法和技巧随着技术框架的不断更新,一些公司摒弃了原先的技术框架模式。而springboot慢慢取代了原有的ssm框架开发。为什么选择springboot呢?配置简单不需要编写太多的xml; 基于spring构建,容易上手; 独立运行不依赖于容器; 内置服务器tomcat不需要打war包; 提供maven极简配置; 对于各种框架有很好的集成; 为SpringCloud微服务奠定基础,使微服务构建变得简单;下面让我们使用idea一起搭建完整版的SpringBoot项目:一.创建SpringBoot项

    2025年8月29日
    10
  • gradle 打包命令(怎么在mac终端输入命令)

    Gradle终端命令给App打包估计好多兄弟们都习惯了用AndroidStudio和Eclipse的build直接对app进行打包,突然有一天领导让用Gradle打包,就措手不及了,我为大家附上方法:首先,需要有一个本地的Gradle,在此我附上网址,是我自己传到网盘上的,所以不是最新的,不影响使用gradle-2.4-all链接:http://pan.baidu.com/s/1nvRcDV7密

    2022年4月15日
    45
  • 18.自定义标签及模板中的使用【由浅入深】

    18.自定义标签及模板中的使用【由浅入深】自定义标签 第一种 自定义普通标签 1 自定义标签的编写 common extras py 文件的编写 fromdatetime 显示当前时间 第一种 在模板中使用自定义标签时传递参数 register simple tagdefcurren time1 format string returndateti now strftime format string 第二种 注册标签时使用 takes context True 则可以

    2026年3月18日
    1
  • 如何屏蔽SIGPIPE信号

    如何屏蔽SIGPIPE信号#include<stdlib.h>#include<sys/signal.h>//默认读写一个关闭的socket会触发sigpipe信号该信号的默认操作是关闭进程这明显是我们不想要的//所以我们需要重新设置sigpipe的信号回调操作函数比如忽略操作等使得我们可以防止调用它的默认操作//信号的处理是异步操作也就是说在这一条语句以后继续往下执行中如果碰到信号依旧会调用信号的回调处理函数//处理sigpipe信号voidhandle_for_s

    2022年5月30日
    36
  • AI Agent框架之争:盘点8大AI Agent开发框架的核心技术与工业级应用

    AI Agent框架之争:盘点8大AI Agent开发框架的核心技术与工业级应用

    2026年3月15日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号