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


相关推荐

  • windows怎么设置多用户_windows开启多用户远程桌面

    windows怎么设置多用户_windows开启多用户远程桌面在实际使用我们较多使用的都是windows系统的单用户模式,它意味着当我们登录一个用户的时候如果另外一个用户也在登录,那么就得等待另外一个用户退出后才可以登录我们这个用户,但是实际需求中,我们经常会遇

    2022年8月4日
    17
  • tp路由+伪静态+去掉index.php

    tp路由+伪静态+去掉index.php

    2021年9月18日
    37
  • god is a girl 俚语_God will make a way

    god is a girl 俚语_God will make a wayWehumanbeingsarealwaystryingtofindaeverythingoperationofthelaw.So,Ialsodon’tstrangemyselfsometimeswhythis.Thetruth

    2022年10月8日
    0
  • Git 切换分支命令

    Git 切换分支命令从Github上clone下来的项目都是主分支branch,为了开发的安全性,如何切换到其它分支呢?gitbranch查看本地分支*表示当前所处的分支,如下图所示:gitbranch-a查看项目所有分支:gitcheckout-b切换分支,例如我切换到stardard-base-4.x-dev:gitcheckout-bstandard-base-4.x-devorigin/standard-base-4.x-dev第二次切换直接填入分支名称即可。

    2022年6月20日
    77
  • vmware linux安装_vm如何安装系统

    vmware linux安装_vm如何安装系统火眼发布Windows渗透工具包(CommandoVM)包含140个渗透工具工具下载地址:github.com/fireeye/commando-vmKaliLinux已成为攻击型安全专家的标配工具,但对需要原生Windows功能的渗透测试员来说,维护良好的类似工具集却是不存在的。安全服务公司火眼就是要改变这一现状。3月28日,该公司发布了一个包含超过140个开源Windows工具的…

    2022年10月1日
    0
  • centos 安装 python3_python加密解密库

    centos 安装 python3_python加密解密库今天使用pip3installvirtualenv命令安装virtualenv的时候一直安装不了,

    2022年9月15日
    0

发表回复

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

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