Android 自定义 declare-styleable 的format

本文详细介绍了在Android开发中如何自定义XML样式属性,包括不同类型的属性定义方式及其调用方法,并提供了完整的自定义属性步骤及示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

declare-styleable 格式定义方法一览表
类型描述             ​字段名称            定义方式举例             调用方式举例            
资源ID
           
reference
           
<attr 
          name = "background"
         format = "reference"
/>

               

<ImageView
    android:layout_width = "42dip"
    android:layout_height = "42dip"
    android:background = "@drawable/图片ID"
    />
颜色 color
<attr 
          name = "background"
         format = "color"
/><span style="font-family: Arial, Helvetica, sans-serif; widows: auto; background-color: rgb(255, 255, 255);">  </span>
<ImageView
    android:background = "#ffffff"
    />
布尔值 boolean
<attr 
          name = "isture"
         format = "boolean"
/>
<ImageView
    app:istrue = "false"
    />
尺寸值
           
dimension
<attr
    name="layout_width"
    format="dimension"
/>
<ImageView
    android:layout_width="42dip"
/>

               

浮点值
           
float  
<attr
    name="fromAlpha"
    format="float"
/>
<View
    app:fromAlpha="1.0"
/>
整数型 integer
<attr
    name="numerical"
    format="integer"
/>
<View
    app:numerical="1"
/>
字符串 string
           
<attr
    name="view_title"
    format="string"
/>
<View
    app:view_title= "open view title"
/>
百分比 fraction
<attr
    name="provX"
    format="fraction"
/><span style="font-family: Arial, Helvetica, sans-serif; widows: auto; background-color: rgb(255, 255, 255);"> </span>
<View
    app:provX="20%"
/>
枚举类型 enum
           
<attr name="orientation">
 <enum name="hori" value="0"/>
 <enum name="ver" value="1"/>
</attr>
<View
    app:orientation="hori"
/>
位或运算 flag
<attr name="orie">
 <flag name="hori" value="0x10"/>
 <flag name="ver" value="0x20"/>
</attr>
<View
    app:orie="hori | ver"
/>
     


附加:自定义xml属性步骤

1、在res/values/目录中新建resources的xml文件,xml举例:
<resources>
    <declare-styleable name="accItem">
        <attr name="accleftimg" format="reference"/>
        <attr name="acctitle" format="string"/>
        <attr name="splitstyle_top">
            <enum name="none_line" value="0"/>
            <enum name="whole_line" value="1"/>
            <enum name="half_line" value="2"/>
        </attr>
        <attr name="splitstyle_bottom">
            <enum name="none_line" value="0"/>
            <enum name="whole_line" value="1"/>
            <enum name="half_line" value="2"/>           
        </attr>
    </declare-styleable>
</resources>

  • declare-styleable:自定义属性块标识

  • accItem:自定义属性块的名称,在代码获取xml属性的时候会使用到

  • <attr>:一项自定义属性的标识块,其中name为属性名称,format为属性格式,参考起始表

2、在xml文件中如何使用
  •     在xml定义xmlns,举例示意:     

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/package"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
</LinearLayout>

  • 代码第二行即为我们自定义的xmlns,有三点需要注意:1、xmlns为定义xmlns标识,不可改变;2、app(xmlns:app)为引用标识,用户可自行改变,使用xml布局时,使用该标识可指定属性,如“app.acctitle = "demo"”;3、package是可变参数,必须于工程项目的包名相同,即"AndroidManifest.xml"中的package     

  •    布局文件中使用自定义控件的时候,和嵌入普通布局一样,引用属性时,使用上一步定义的xmlns加第一步时定义的attr->name直接使用。举例说明:

    <!--自定义控件-->
    <package.view.demo
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        <!--app为我们定义的xmlns-->
        <!--app后的属性已在declare-styleable中定义-->
        app:accleftimg="@drawable/gerenziliaoimg"
        app:acctitle="@string/gerenziliao"
     
        app:splitstyle_top="whole_line"
        app:splitstyle_bottom="half_line"
        >               
    </package.view.demo>

    参考代码:3、如何在自定义控件中获取自定义属性的值
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.accItem);
    mheaderDrawable = a.getDrawable(R.styleable.accItem_accleftimg);
    mcontentTitle = a.getString(R.styleable.accItem_acctitle);
    mtopSpliterStyle = a.getInt(R.styleable.accItem_splitstyle_top, 0);
    mbottomSpliterStyle = a.getInt(R.styleable.accItem_splitstyle_bottom, 0);
    a.recycle();

    第一行:R.styleable.accItem,其中accItem为在资源文件中定义的declare-styleable代码块的name属性使用TypedArray 获取自定义属性

    • 第二行:R.styleable.accItem_accleftimg,其中accItem_accleftimg为[declare-styleable][name]->[attr][name]属性

      第三行:执行完毕后,需要调用recycle来释放元素

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值