一. 什么是自定义XML属性

在我们使用自定义的控件时,很多时候都需要定义一些不同于一般的XML属性前缀(如android:layout_width)的属性,比如这样 app:textColor,这些就是自定义控件需要用到的自定义控件属性。

二. 自定义XML属性有什么用

自定义XML属性的作用在于,在采取自定义的控件时,很多时候,系统的一般XML属性已经不能满足需求,比如我们在做一个具有描边效果的TextView时,就需要有额外定义的TextView外边框颜色和TextView内部颜色两种颜色。这时候,使用自定义XML属性,用户就可以很方便地在XML中配置额外的属性。

三. 怎么使用自定义XML属性

1.定义对应的属性 在values文件夹下新建一个attar_custom.xml文件: <?xml version="1.0" encoding="utf-8"?> <resources> <!-- 自定义控件的名称 --> <declare-styleable name="StrokeTextView"> <!-- 自定义的属性名称 和对应的单位 --> <attr name="outerColor" format="color|reference" /> <attr name="innnerColor" format="color|reference" /> </declare-styleable> </resources> 2.在XML中定义自定义属性 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.demo.StrokeTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:textSize="28sp" app:outerColor="#000000" app:innnerColor="#ffffff" android:layout_centerInParent="true"/> </RelativeLayout> 注意,自定义的XML属性必须给自定义的控件使用。