自定义属性,资源文件attrs.xml

时间:2023-03-09 08:25:48
自定义属性,资源文件attrs.xml

1.attrs.xml中写:在values文件夹下。

  

<?xml version="1.0" encoding="utf-8"?>
<resources> <declare-styleable name="TestAttr">
<attr name="name" format="reference" />
<attr name="age">
<flag name="child" value="10" />
<flag name="young" value="18" />
<flag name="oldman" value="60" />
</attr>
<attr name="textSize" format="dimension" />
</declare-styleable>
</resources>

2.布局文件中写:

  

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:attrstest="http://schemas.android.com/apk/res/com.arlos.attrstest"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >s <com.arlos.attrstest.MyTestView
android:id="@+id/tvTest"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
attrstest:name="@string/myname"
android:gravity="top"
attrstest:age="young"
attrstest:textSize="@dimen/aa"
android:text="@string/hello" /> </LinearLayout>