• 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>
  • 相关阅读:
    php 中 return exit break contiue 详解
    C++ Primer学习笔记2--c++标准库中的 vector、string 和 bitset 类型
    asp.net 生成xml文件 与 asp生成xml文件
    android 性能优化
    Internet基础
    华为一道机试
    智能家居趋势
    hdu4708 Rotation Lock Puzzle
    java要在命令行执行eclipse的项目的方法
    linux和windows下安装python拓展包及requirement.txt安装类库
  • 原文地址:https://www.cnblogs.com/tianzhijiexian/p/3854639.html
Copyright © 2020-2023  润新知