@[toc]php
自定义属性其实就是一些 xml 标签,他们经过 xml 文件的形式,能够配置某些 View 的信息,让自定义 View 使用起来更加灵活。java
想必不少同窗都已经对于自定义属性使用的驾轻就熟了,可是有一些细节你真的知道吗?好比 AttributeSet、TypedArray 、declare-styleable 这些类和标签的内容你都清楚吗,在获取自定义属性的时候为何要用android
Context.obtainStyledAttributes(AttributeSet, R.styleable.XXXX);
复制代码
方法呢?全部的答案都会在这篇文章里。git
不少文章都说:须要在 res/values 目录下建立 attrs.xml 文件而后在里面写咱们须要的属性,其实这是不太准确的,经过实验证实,文件的名字能够随意指定,不必定必须是 attrs.xml !github
例如笔者自定义了一个 custom.xml 文件, 里面的内容符合自定义属性的规范,在 View 中也是能够正常访问到的。(具体缘由尚不清楚,多是 Android Stuido 的功能)app
name space : 命名空间,名字能够随便起,可是最好和自定义 View 的名字相同,由于 Android Stuido 能够帮咱们作一些事情,好比说 command + 手表左键,能够直接跳转。布局
attr name :这就是咱们自定义属性的名字,具体的格式仍是模仿 android 内部的方式,驼峰式命名或者是 名称_名称学习
format : 属性的具体类型,此处讲解一些特殊的类型,此处不是重点,网上文章不少。ui
a .reference: 资源idthis
<ImageView android:background = "@drawable/图片ID"/>
复制代码
b. fraction : 百分数
<attr name="pivotX" format="fraction"/>
复制代码
<android:pivotX = "200%"/>
复制代码
c. flag : 位运算,能够在使用过程当中指定多个值
<attr name="gravity" format="flags">
<flag name="top" value="0x30"/>
<flag name="bottom" value="0x50" />
<flag name="left" value="0x03" />
<flag name="right" value="0x05" />
<flag name="center_vertical" value="0x10" />
</attr>
复制代码
<TextView android:gravity="bottom|left"/>
复制代码
d. enum : 枚举
<attr name="orientation" format="enum">
<enum name="horizontal" value="0"/>
<enum name="vertical" value="1"/>
</attr>
复制代码
e. 混合模式 :指定属性的时候能够指定多种类型值
<attr name="background" format="reference|color"/>
复制代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomAttrsDemo">
<attr name="text_color" format="color" />
<attr name="text" format="dimension" />
</declare-styleable>
</resources>
复制代码
在 xml 文件中使用
在布局文件中使用,首先须要引入命名空间,这样才能找到咱们包中的 attrs,这里咱们引入了命名空间 app,res-auto 表示自动查找
xmlns:app="http://schemas.android.com/apk/res-auto"
复制代码
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.example.dsd.demo.ui.draw.attrs.CustomAttrsDemo
android:id="@+id/custom_attrs_demo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
app:text_color="#333333"
app:text_size="10sp"/>
</FrameLayout>
复制代码
/** * 自定义属性 Demo * * Created by im_dsd on 2019-08-11 */
public class CustomAttrsDemo extends android.support.v7.widget.AppCompatTextView {
private final int mTextColor;
private final int mTextSize;
public CustomAttrsDemo(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomAttrsDemo);
mTextColor = array.getColor(R.styleable.CustomAttrsDemo_textColor, Color.BLACK);
mTextSize = array.getDimensionPixelSize(R.styleable.CustomAttrsDemo_textSize, 18);
// 注意使用完成以后必定要回收
array.recycle();
}
}
复制代码
AttributeSet :
A collection of attributes, as found associated with a tag in an XML document. Often you will not want to use this interface directly, instead passing it to {@link android.content.res.Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int) Resources.Theme.obtainStyledAttributes()}
能够看到 AtttirbuteSet 是一个大的属性集合,装载了此 View 全部的属性,用户能够经过方法:
Context.obtainStyledAttributes(AttributeSet, R.styleable.XXXX);
复制代码
获取指定的属性集合(一个明确的小集合 TypedArray)
TypedArray
TypedArray array = Context.obtainStyledAttributes(AttributeSet, R.styleable.XXXX);
复制代码
TypedArray里面装的就是具体的属性了,咱们能够经过 :array.getXXXX
的方法获取具体的属性值
注意: 在使用后必定要调用array.recycle
用于释放内存空间,否则此内存空间就被浪费了
declare-styleable
此标签的做用就是将属性分组,在 Context.obtainStyledAttributes
方法中指定须要加载的属性组
此次自定义属性就完成了。
自定义属性仍是很简单的,可是不少同窗都把加载自定义属性的过程当错了模版代码背了下来,不明白的其中的道理,在使用过程当中仍是很难达到灵活运用。
TypeArray array = Context.obtainStyledAttributes(AttributeSet, R.styleable.属性集合名);
复制代码
获取指定的属性集 R.styleable.属性集合名
,而后经过方法:
array.getXXXX(R.styleable.属性集合名_属性名, 默认值)的方式获取属性
array.recyler()
复制代码
方法获取属性值
版权声明:禁止一切商业行为,转载请注明出处 mp.csdn.net/mdeditor/99… 做者:代圣达