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

Posted on 2015-08-10 10:21  小小蜗牛灬  阅读(406)  评论(0编辑  收藏  举报

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.布局文件中写:

  

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:attrstest="http://schemas.android.com/apk/res/com.arlos.attrstest"
 4     android:layout_width="fill_parent"
 5     android:layout_height="fill_parent"
 6     android:orientation="vertical" >s
 7 
 8     <com.arlos.attrstest.MyTestView
 9         android:id="@+id/tvTest"
10         android:layout_width="fill_parent"
11         android:layout_height="wrap_content"
12          attrstest:name="@string/myname" 
13          android:gravity="top"
14          attrstest:age="young"
15          attrstest:textSize="@dimen/aa"
16         android:text="@string/hello" />
17 
18 </LinearLayout>
View Code

 

Copyright © 2024 小小蜗牛灬
Powered by .NET 8.0 on Kubernetes