attrs.xml中declare-styleable 详解(用于自定义控件的属性)

 

1. 框架定义:

<declare-styleable name = "名称">

  <attr name = "……" format = "……" />

</declare-styleable>

 

2. color:颜色值,指定这个属性必须输入的是颜色值

<attr name = "textColor" format = "color" />

 

3. boolean:布尔值,指定这个属性必须输入的是boolean类型(true/false)

<attr name = "focusable" format = "boolean" />

 

4. dimension:尺寸值。注意,这里如果是dp那就会做像素转换为dip

<attr name = "layout_width" format = "dimension" />

 

5. float:浮点值。

<attr name="degree" format="float"></attr> 

 

6. integer:整型值。

<attr name="startAngle" format="integer"></attr> 

 

7. string:字符串

<attr name="text" format="string"></attr> 

 

8. fraction:百分数。使用: android:pivotY = "300%" 

<attr name = "pivotY" format = "fraction" />

 

9. enum:枚举值,设置这个属性必须输入的值。比如style类型,就只能输入STROKE/FILL。在于代码链接的过程中就是传0/1

<attr name="style">
  <enum name="STROKE" value="0"></enum>
  <enum name="FILL" value="1"></enum>
</attr>

 

10. flag:是自己定义的,类似于 android:gravity="top",就是里面对应了自己的属性值。

 <attr name="weight">              

  <flag name="fat" value="0" />             

   <flag name="mid" value="1" />              

  <flag name="thin" value="2" />          

</attr>  

 

11. reference|color:颜色的资源文件。

12.reference|boolean:布尔值的资源文件

注意:由于reference是从资源文件中获取:所以在XML文件中写这个属性的时候必须 personattr:name="@string/app_name"这种格式,否则会出错

 

     属性定义时可以指定多种类型值。

    (1)属性定义:

            <declare-styleable name = "名称">

                   <attr name = "background" format = "reference|color" />

            </declare-styleable>

    (2)属性使用:

             <ImageView

                     android:layout_width = "42dip"
                     android:layout_height = "42dip"
                     android:background = "@drawable/图片ID|#00FF00" />

 

下面将代码和定义的xml文件联系起来

TypedArray tArray = context.obtainStyledAttributes(attrs,R.styleable.PersonAttr);//获取配置属性  

自定义变量age,通过TypedArray 对象来获取xml中国的值。如果用户在使用该控件的时候有定义age属性的值,那么就得到用户定义的值,否则就用第二个参数作为默认值,即:如果没定义,那么默认为age = 15

int age = tArray.getInt(R.styleable.PersonAttr_age, 15);  

 

使用前,需要在该控件或者是他的父控件中声明命名空间:

xmlns:personattr="http://schemas.android.com/apk/res/com.example.declare_styleable"

xmlns:自定义的空间名="http://schemas.android.com/apk/res/自定义视图类所在项目的包名"

 

比如自定义的类叫Rotate,包名是com.example.declare_styleable,那么就按如下方式来使用该控件

<com.example.declare_styleable.Rotate

                   xmlns:pt = "http://schemas.android.com/apk/res/com.example.declare_styleable" 
               pt:interpolator = "@anim/动画ID"

                   pt:fromDegrees = "0" 
               pt:toDegrees = "360"

                   pt:pivotX = "200%"

                   pt:pivotY = "300%" 
               pt:duration = "5000"  />

 

小例子:

attrs.xml(定义)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="PersonAttr">
        <attr name="name" format="reference" />
        <attr name="sex" format="reference" />
        <attr name="age" format="integer" />
        <attr name="weight">
            <flag name="fat" value="2" />
            <flag name="mid" value="1" />
            <flag name="thin" value="0" />
        </attr>
        <attr name="adult" format="boolean" />
        <attr name="textSize" format="dimension" />
    </declare-styleable>
</resources>

layout.xml(使用)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:personattr="http://schemas.android.com/apk/res/com.example.declare_styleable"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.example.declare_styleable.PersonView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
personattr:name="@string/person_name" personattr:weight ="fat" personattr:adult ="false" personattr:textSize="@dimen/text_size"/> </RelativeLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Android 开发attrs.xml 是一个非常重要的文件,它用于定义自定义属性,可以让我们在布局文件直接使用这些属性。 下面是一个示例 attrs.xml 文件: ```xml <?xml version="1.0" encoding="utf-8"?> <resources> <attr name="myText" format="string" /> <attr name="myTextColor" format="color" /> <attr name="myBackgroundColor" format="color" /> </resources> ``` 这个文件定义了三个自定义属性:myText、myTextColor 和 myBackgroundColor。其,name 属性指定属性的名称,format 属性指定属性的类型。 在布局文件使用自定义属性的方法是: ```xml <com.example.MyView android:layout_width="match_parent" android:layout_height="wrap_content" app:myText="Hello World!" app:myTextColor="@color/red" app:myBackgroundColor="@color/white" /> ``` 在 MyView 类,可以通过如下方法获取这些自定义属性的值: ```java TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyView); String myText = a.getString(R.styleable.MyView_myText); int myTextColor = a.getColor(R.styleable.MyView_myTextColor, Color.BLACK); int myBackgroundColor = a.getColor(R.styleable.MyView_myBackgroundColor, Color.WHITE); a.recycle(); ``` 其,obtainStyledAttributes() 方法获取了这些属性的值,getString() 和 getColor() 方法获取了对应属性的值,recycle() 方法回收 TypedArray 对象。 通过自定义属性,我们可以让布局文件更加简洁明了,代码也更加易于维护。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值