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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Makefile中的patsubst函数[通俗易懂]

    $(patsubst原模式,目标模式,文件列表)如:在$(patsubst%.c,%.o,$(dir))中,patsubst把$(dir)中的变量符合后缀是.c的全部替换成.o$(patsubstpattern,replacement,text)表示寻找text中符合模式pattern的字,用replacement替换他们。%是Makefile的通配符…

    2022年4月18日
    124
  • DP和HDMI区别「建议收藏」

    转自:https://www.toutiao.com/i6877677362054595080在目前市面上显示器接口中,VGA和DVI已经逐渐退出了历史舞台,Type-C还算是小众,而DP(DisplayPort)与HDMI则成为了主流产品的标配,目前的主流级显卡也是以这两个输出接口为主,而新的问题也随之诞生了:当这两个接口都可以使用的时候,选择哪个会更好?对于大部分普通的消费者来说,显示器能跟主机正常连接就行,随便哪个接口都无所谓,反正能正常使用,但是对于DIY玩家来说,这个问题就显得非常重要

    2022年4月16日
    171
  • FileSystemWatcher触发多次Change事件的解决办法

    FileSystemWatcher触发多次Change事件的解决办法FileSystemWatcher监视文件夹中数据变化时,会多次出发Change事件。EnableRaisingEvents=true,启动文件监控,网上查到的做法是通过一个定时器来避免多次触发。其实可以通过,在第一次触发后EnableRaisingEvents=false先关闭在打开EnableRaisingEvents=true,简单解决这个问题。

    2022年6月18日
    27
  • ora01017 linux,ORA-01017: invalid username/password; logon denied 解决办法

    ora01017 linux,ORA-01017: invalid username/password; logon denied 解决办法ORA-01017:invalidusername/password;logondenied解决办法环境介绍:操作系统RHEL6.4X64、数据库:ORACLE11.2.0.4.0;在现有环境下手动创建数据库,在数据库的手动创建过程中没有大的问题,但在数据库创建完成后出现在本地可以登录数据库用户包括管理员账户,通过客户端远端无法登录报如下错误:通过测试,或者在数据库系统中更换用户账户口…

    2022年5月6日
    275
  • mybatis:Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource[通俗易懂]

    mybatis:Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource[通俗易懂]整天写业务逻辑代码,但偶尔整个配置搞死人(根基不牢),有些细节知识还是欠缺,遇到问题总是搞的很烦躁,通过这篇博文将自己遗忘的知识总结起来。_____________________________________________________________________________________________先贴错误:看起来很简单,按照错误排查一下,就ok,但硬生生搞了半天,还搞的烦躁,这么简单,咋找不到问题呢。分析:看错误可知,找不到mapper文件。查看myb.

    2022年6月13日
    37
  • 如何用公式编辑器打求和符号?

    如何用公式编辑器打求和符号?作为使用最广泛的公式编辑器,MathType中所包含的很多符号能够满足我们一般的学习工作需要,利用它能够毫不费力地编辑出各种各样的数学公式,除了常见的代数几何符号,集合符号以及概率统计等一些符号也有,还有一些模板可以直接使用。求和符号是概率统计中经常用到的一个符号,下面就来介绍用MathType打求和符号的方法。

    2022年8月31日
    6

发表回复

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

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