Android自定义控件属性(草稿版)

常用的format类型:

1)  string:字符串类型;
2)  integer:整数类型;
3)  float:浮点型;
4)  dimension:尺寸,后面必须跟dp、dip、px、sp等单位;
5)  Boolean:布尔值;
6)  reference:引用类型,传入的是某一资源的ID,必须以“@”符号开头;
7)  color:颜色,必须是“#”符号开头;
8)  fraction:百分比,必须是“%”符号结尾;
9)  enum:枚举类型
  <declare-styleable name="circleView">
        <attr name="textSize" format="dimension" />
        <attr name="text" format="string" />
        <attr name="circleColor" format="color" />
        <attr name="arcColor" format="color" />
        <attr name="textColor" format="color" />
        <attr name="startAngle" format="integer" />
        <attr name="sweepAngle" format="integer" />
    </declare-styleable>
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="rightPadding" format="dimension" />

    <declare-styleable name="CustomMenu">
        <attr name="rightPadding" />
    </declare-styleable>
</resources> 

<resources>
    <attr name="orientation">
        <enum name="horizontal" value="0" />
        <enum name="vertical" value="1" />
    </attr>

    <declare-styleable name="CustomView">
        <attr name="orientation" />
    </declare-styleable>
</resources>

在XML布局文件中使用自定义的属性时,我们需要先定义一个namespace。Android中默认的namespace是android,因此我们通常可以使用“android:xxx”的格式去设置一个控件的某个属性,android这个namespace的定义是在XML文件的头标签中定义的,通常是这样的:

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

  我们自定义的属性不在这个命名空间下,因此我们需要添加一个命名空间。

  自定义属性的命名空间如下:

xmlns:app="http://schemas.android.com/apk/res-auto"

  可以看出来,除了将命名空间的名称从android改成app之外,就是将最后的“res/android”改成了“res-auto”。

  注意:自定义namespace的名称可以自己定义,不一定非得是app。

猜你喜欢

转载自blog.csdn.net/yh18668197127/article/details/84999508
今日推荐