前言:可是有时候我们总感觉官方定义的一些基本组件不够用,自定义组件就不可避免了。那么如何才能做到像官方提供的那些组件一样用xml来定义他的属性呢?
先总结下自定义View的步骤:
1、自定义View的属性;
2、在View的构造方法中获得自定义的属性。
一、在res/values文件下定义一个attrs.xml文件,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyView"><!--定义属性名和格式的地方-->
<attr name="rect_color" format="color"/>
</declare-styleable>
</resources>
二、在自定义组件中,可以如下获得xml中定义的值:
public class MyRect extends View {
public MyRect(Context context) {
super(context);
}
//资源解析器解析的方法
public MyRect(Context context,AttributeSet attrs) {
super(context, attrs);
//TypedArray获得对属性集的引用
TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.MyView);
//对属性的设置
int color=ta.getColor(R.styleable.MyView_rect_color,0xffff0000);
setBackgroundColor(color);
ta.recycle();//进行回收
}
}
三、在布局xml中如下使用该属性:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="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.customview.MyRect
android:layout_width="100dp"
android:layout_height="100dp"
custom:rect_color="#ff0000ff"/>
</LinearLayout>
就这么简单的三步,即可完成对自定义属性的使用。
*********************************************************************
好了,基本用法已经讲完了,现在来看看一些注意点和知识点吧。
首先来看看attrs.xml文件.............
该文件是定义属性名和格式的地方,需要用<declare-styleable name="MyView"></declare-styleable>包围所有属性。其中name为该属性集的名字,主要用途是标识该属性集。那在什么地方会用到呢?主要是在第三步。看到没?在获取某属性标识时,用到"R.styleable.MyView",很显然,他在每个属性前面都加了"MyView_"。
在来看看各种属性都有些什么类型吧:string , integer , dimension , reference , color , enum......
前面几种的声明方式都是一致的,例如:<attr name="rect_color" format="color"/>。
只有enum是不同的,用法举例:
<attr name="testEnum">
<enum name="fill_parent" value="-1"/>
<enum name="wrap_content" value="-2"/>
</attr>
如果该属性可同时传两种不同的属性,则可以用“|”分割开即可。
让我们再来看看布局xml中需要注意的事项。
首先得声明一下:xmlns:custom="http://schemas.android.com/apk/res-auto"
注意,“custom”可以换成其他的任何名字,后面的url地址Gradle就会自动查找自定属性。自定义属性了,在属性名前加上“custom”即可。
最后来看看java代码中的注意事项。
在自定义组件的构造函数中,用
TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.MyView);
来获得对属性集的引用,然后就可以用“a”的各种方法来获取相应的属性值了。这里需要注意的是,如果使用的方法和获取值的类型不对的话,则会返回默认值。因此,如果一个属性是带两个及以上不用类型的属性,需要做多次判断,知道读取完毕后才能判断应该赋予何值。当然,在取完值的时候别忘了回收资源哦!
自定义属性数据类型简介:
一、reference:参考指定Theme中资源ID。
1.定义:
1 2 3 | <declare-styleable name="My"> <attr name="label" format="reference" > </declare-styleable> |
2.使用:
1 | <Button zkx:label="@string/label" > |
二、Color:颜色
1.定义:
1 2 3 | <declare-styleable name="My"> <attr name="textColor" format="color" /> </declare-styleable> |
2.使用:
1 | <Button zkx:textColor="#ff0000"/> |
三、boolean:布尔值
1.定义:
1 2 3 | <declare-styleable name="My"> <attr name="isVisible" format="boolean" /> </declare-styleable> |
2.使用:
1 | <Button zkx:isVisible="false"/> |
四、dimension:尺寸值
1.定义:
1 2 3 | <declare-styleable name="My"> <attr name="myWidth" format="dimension" /> </declare-styleable> |
2.使用:
1 | <Button zkx:myWidth="100dip"/> |
五、float:浮点型
1.定义:
1 2 3 | <declare-styleable name="My"> <attr name="fromAlpha" format="float" /> </declare-styleable> |
2.使用:
1 | <alpha zkx:fromAlpha="0.3"/> |
六、integer:整型
1.定义:
1 2 3 | <declare-styleable name="My"> <attr name="frameDuration" format="integer" /> </declare-styleable> |
2.使用:
1 | <animated-rotate zkx:framesCount="22"/> |
七、string:字符串
1.定义:
1 2 3 | <declare-styleable name="My"> <attr name="Name" format="string" /> </declare-styleable> |
2.使用:
1 | <rotate zkx:pivotX="200%"/> |
八、fraction:百分数
1.定义:
1 2 3 | <declare-styleable name="My"> <attr name="pivotX" format="fraction" /> </declare-styleable> |
2.使用:
1 | <rotate zkx:Name="My name is zhang kun xiang"/> |
九、enum:枚举
1.定义:
1 2 3 4 5 | <declare-styleable name="My"> <attr name="language"> <enum name="English" value="1"/> </attr> </declare-styleable> |
2.使用:
1 | <Button zkx:language="English"/> |
十、flag:位或运算
1.定义:
1 2 3 4 5 6 | <declare-styleable name="My"> <attr name="windowSoftInputMode"> <flag name="stateUnspecified" value="1" /> <flag name = "adjustNothing" value = "0x30" /> </attr> </declare-styleable> |
2.使用:
1 | <activity android:windowSoftInputMode="stateUnspecified | adjustNothing"> |
属性定义时可以指定多种类型值:
1 2 3 | <declare-styleable name = "名称"> <attr name="background" format="reference|color" /> </declare-styleable> |
使用:
1 | <ImageView android:background = "@drawable/图片ID|#00FF00"/> |
参考博客:https://blog.youkuaiyun.com/lmj623565791/article/details/24252901