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

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

<declare-styleable name = "名称">

<attr name = "cutom_id" format = "reference" />

</declare-styleable>

(2)属性使用:

<CustomView

android:layout_width = "wrap_content"
                     android:layout_height = "wrap_content"
                     app:cutom_id = "@drawable/图片ID"

/>

  5.2. color:颜色值。

(1)属性定义:

<declare-styleable name = "名称">

<attr name = "custom_color" format = "color" />

</declare-styleable>

(2)属性使用:

<CustomView

android:layout_width = "wrap_content"
                     android:layout_height = "wrap_content"
                     app:custom_color = "#00FF00"

/>

  5.3. boolean:布尔值。

(1)属性定义:

<declare-styleable name = "名称">

<attr name = "custom_b" format = "boolean" />

</declare-styleable>

(2)属性使用:

<CustomView

android:layout_width = "wrap_content"
                    android:layout_height = "wrap_content"

app:custom_b = "true"

/>

  5.4. dimension:尺寸值。

(1)属性定义:

<declare-styleable name = "名称">

<attr name = "custom_width" format = "dimension" />

</declare-styleable>

(2)属性使用:

<CustomView

android:layout_width = "wrap_content"
                    android:layout_height = "wrap_content"

       app:custom_width="44dp"

/>

  5.5. float:浮点值。

(1)属性定义:

<declare-styleable name = "名称">

<attr name = "custom_alpha" format = "float" />

</declare-styleable>

(2)属性使用:

<CustomView
              android:layout_width = "wrap_content"

 android:layout_height = "wrap_content"

       app:custom_alpha="0.5"

/>

  5.6. integer:整型值。

(1)属性定义:

<declare-styleable name = "名称">

<attr name = "custom_number" format="integer" />

</declare-styleable>

(2)属性使用:

<CustomView

       android:layout_width = "wrap_content"

  android:layout_height = "wrap_content"

       app:custom_number="5"

/>

  5.7. string:字符串。

(1)属性定义:

<declare-styleable name = "名称">
                   <attr name = "custom_key" format = "string" />
            </declare-styleable>

(2)属性使用:

<CustomView
                    android:layout_width = "wrap_content"
                    android:layout_height = "wrap_content"
                    app:custom_key = "test_msg"

/>

  5.8. fraction:百分数。

(1)属性定义:

<declare-styleable name="名称">
                   <attr name = "custom_percent" format = "fraction" />
            </declare-styleable>

(2)属性使用:

<CustomView

android:layout_width = "wrap_content"
                 android:layout_height = "wrap_content"

app:custom_percent = "200%"

/>

  5.9. enum:枚举值。

(1)属性定义:

<declare-styleable name="名称">
                   <attr name="custom_orientation">
                          <enum name="horizontal" value="0" />
                          <enum name="vertical" value="1" />
                   </attr>

</declare-styleable>

(2)属性使用:

<CustomView

android:layout_width = "wrap_content"
                    android:layout_height = "wrap_content"

app:custom_orientation = "vertical"
            />

  5.10. flag:位或运算。

(1)属性定义:

<declare-styleable name="名称">
                    <attr name="custom_mode">
                            <flag name = "mode_one" value = "0" />
                            <flag name = "mode_two" value = "1" />
                            <flag name = "mode_three" value = "2" />
                     </attr>

</declare-styleable>

(2)属性使用:

<CustomView

android:layout_width = "wrap_content"
                    android:layout_height = "wrap_content"

app:custom_mode = "mode_one|mode_two|mode_three"
            />

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

    (1)属性定义:

<declare-styleable name = "名称">

<attr name = "custom_background" format = "reference|color" />

</declare-styleable>

(2)属性使用:

<CustomView

android:layout_width = "wrap_content"
                     android:layout_height = "wrap_content"
                     app:custom_background = "@drawable/图片ID|#00FF00"

/>

关于Android attrs 自定义属性的说明的更多相关文章

  1. Android中自定义属性(attrs.xml,TypedArray的使用)

    做Android布局是件很享受的事,这得益于他良好的xml方式.使用xml可以快速有效的为软件定义界面.可是有时候我们总感觉官方定义的一些基本组件不够用,自定义组件就不可避免了.那么如何才能做到像官方 ...

  2. android开发:Android 中自定义属性(attr.xml,TypedArray)的使用

    今天我们的教程是根据前面一节扩展进行的,如果你没有看,请点击 Android高手进阶教程(三)查看第三课,这样跟容易方便你的理解! 在xml 文件里定义控件的属性,我们已经习惯了android:att ...

  3. Android之自定义属性

    有些时候会觉得Android中提供的控件不能满足项目的要求,所以就会常常去自定义控件.自定义控件就不免会自定义属性.自定义属性大致需要三个步骤:在XML文件中定义自定义属性的名称和数据类型.在布局中调 ...

  4. Android中自定义属性的使用

    做Android布局是件很享受的事,这得益于他良好的xml方式.使用xml可以快速有效的为软件定义界面.可是有时候我们总感觉官方定义的一些基本组件不够用,自定义组件就不可避免了.那么如何才能做到像官方 ...

  5. Android attrs.xml文件中属性类型format值的格式

    "reference" //引用 "color" //颜色 "boolean" //布尔值 "dimension" // ...

  6. Android中自定义属性attr.xml的格式详解

    1. reference:参考某一资源ID.     (1)属性定义:             <declare-styleable name = "名称">      ...

  7. Android 自定义属性(attrs.xml,TypedArray)

    做Android布局是件很享受的事,这得益于他良好的xml方式.使用xml可以快速有效的为软件定义界面.可是有时候我们总感觉官方定义的一些基本组 件不够用,自定义组件就不可避免了.那么如何才能做到像官 ...

  8. Android学习笔记_49_Android中自定义属性(attrs.xml,TypedArray的使用)

    做Android布局是件很享受的事,这得益于他良好的xml方式.使用xml可以快速有效的为软件定义界面.可是有时候我们总感觉官方定义的一些基本组件不够用,自定义组件就不可避免了.那么如何才能做到像官方 ...

  9. [置顶] xamarin android自定义标题栏(自定义属性、回调事件)

    自定义控件的基本要求 这篇文章就当是自定义控件入门,看了几篇android关于自定义控件的文章,了解了一下,android自定义控件主要有3种方式: 自绘控件:继承View类,所展示的内容在OnDra ...

随机推荐

  1. 架构师入门:搭建基本的Eureka架构(从项目里抽取)

    没有废话,直接上干货,理论部分大家可以看其它资料. 这里是部分关键代码,如果需要全部可运行的代码,请给本人留言. 在后继,还将给出搭建高可用Eureka架构的方式. 1 Eureka的框架图 在Eur ...

  2. 我不知道的行高——line-height

    概述 对于块级元素,CSS属性line-height指定了元素内部line-boxes的最小高度. 对于非替代行内元素,line-height用于计算line box的高度. 对于替代行内元素,如bu ...

  3. Java集合中的AbstractMap抽象类

    jdk1.8.0_144 AbstractMap抽象类实现了一些简单且通用的方法,本身并不难.但在这个方法中有两个方法非常值得关注,keySet和values方法源码的实现可以说是教科书式的典范. 抽 ...

  4. Caused by: java.lang.ClassCastException: org.springframework.web.SpringServletContainerInitializer cannot be cast to javax.servlet.ServletContainerInitializer错误解决办法

    严重: Failed to initialize end point associated with ProtocolHandler ["http-bio-8080"] java. ...

  5. python——Django项目模板

    views.py # -*- coding: utf-8 -*- from __future__ import unicode_literals from django.shortcuts impor ...

  6. Java集合框架(二)—— HashSet、LinkedHashSet、TreeSet和EnumSet

    Set接口 前面已经简绍过Set集合,它类似于一个罐子,一旦把对象'丢进'Set集合,集合里多个对象之间没有明显的顺序.Set集合与Collection基本上完全一样,它没有提供任何额外的方法. Se ...

  7. nyoj281 整数中的1(二) 数位DP

    和整数中的1一毛一样.就是输入时改了一下罢了. AC代码: #include<cstdio> const int maxn = 35; int w[maxn], h[maxn]; void ...

  8. uploadify上传文件(1)--下载

    最近在给公司做一个软件版本迭代管理的软件,是一个asp.net网站开发项目.利用mvc框架,前端采用bootstrap,数据库是MySQL,数据库访问利用EF框架. 软件需求是公司软件开发项目多,版本 ...

  9. 项目使用EntityFramework需要做的几项工作

    1..新项目引用创建好的其他项目,比如实体类库.数据库业务.实体数据模型等需要用到的项目进行引用. 2.新项目使用NuGet获取AutoMapper和EntityFramework程序包进行安装引用, ...

  10. 不使用spring的情况下用java原生代码操作mongodb数据库的两种方式

    由于更改了mongodb3.0数据库的密码,导致这几天storm组对数据进行处理的时候,一直在报mongodb数据库连接不上的异常.   主要原因实际上是和mongodb本身无关的,因为他们改的是配置 ...