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


相关推荐

  • SQL Server 2008安装图解教程

    SQL Server 2008安装图解教程一、安装SQLServer2008安装SQL2008的过程与SQL2005的程序基本一样,只不过在安装的过程中部分选项有所改变,当然如果只熟悉SQL2000安装的同志来说则是一个革命性的变动。(一)安装前的准备(1)需要.NetFramework3.5,若在Vista或更高的OS上需要3.5SP1的支持(在SQL2008安装的前会自动更新安装)(2)需要Widnows

    2022年6月23日
    27
  • iframe参数[通俗易懂]

    iframe参数[通俗易懂]iframe参数:runat="server"这个最好加上Iframe跳转asp.net可在当前iframe中跳转src链接到的地址URlframeborder是否显示边

    2022年7月4日
    44
  • 工业大数据漫谈5:工业大数据案例(上)

    工业大数据漫谈5:工业大数据案例(上)今天,整理了一些工业大数据的相关案例,看看各家企业是如何成功实施工业大数据的。    1、能够实现全生产过程的信息透明化的案例    通过采用集成自动化与驱动解决方案,能够显著提高生产效率和灵活性。原东德玻璃制造商f|glass就是一个很好的实例。它的工厂可以算得上是全世界最先进、最节能的工厂之一了。该工厂采用了一套集成自动化解决方案、一个先进的能源管理系统以及一个创新

    2022年6月5日
    31
  • python怎么读取xlsx文件_arcgis地理加权回归

    python怎么读取xlsx文件_arcgis地理加权回归空间计量经济学打破大多数经典统计和计量分析中相互独立的基本假设,主要解决如何在横截面数据和面板数据的回归模型中处理空间相互作用(空间自相关)和空间结构(空间不均匀性)分析的问题。空间计量经济理论认为一个地区空间单元上的某种经济地理现象或某一属性值与邻近地区空间单元上同一现象或属性值是相关的。也就是说,各区域之间的数据存在与时间序列相关相对应的空间相关。空间计量模型所研究的空间效应包括空间自相关和空…

    2022年8月31日
    2
  • java list转set用法

    java list转set用法参考:https://blog.csdn.net/kye055947/article/details/80561633之前一直被这两list转set,set转list所烦恼,今天整理了下两者之间怎么转的初始化listList<Integer>list=newArrayList<>();list.add(11);list.add(22);list.add(33);方法一(直接放HashSet里面)Set<Integer>lis

    2022年6月21日
    43
  • 最大公约数(Greatest Common Divisor)

    最大公约数(Greatest Common Divisor)

    2022年1月12日
    39

发表回复

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

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