Android 自定义 declare-styleable 的format

简介: 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来释放元素

目录
相关文章
|
12天前
|
Java API 调度
Android系统 自定义开机广播,禁止后台服务,运行手动安装应用接收开机广播
Android系统 自定义开机广播,禁止后台服务,运行手动安装应用接收开机广播
37 0
|
12天前
|
存储 Java Linux
Android Mstar增加IR 自定义遥控头码完整调试过程
Android Mstar增加IR 自定义遥控头码完整调试过程
24 1
|
27天前
|
缓存 测试技术 Android开发
深入探究Android中的自定义View绘制优化策略
【4月更文挑战第8天】 在Android开发实践中,自定义View的绘制性能至关重要,尤其是当涉及到复杂图形和动画时。本文将探讨几种提高自定义View绘制效率的策略,包括合理使用硬件加速、减少不必要的绘制区域以及利用缓存机制等。这些方法不仅能改善用户体验,还能提升应用的整体性能表现。通过实例分析和性能测试结果,我们将展示如何有效地实现这些优化措施,并为开发者提供实用的技术指南。
|
1月前
|
前端开发 Android开发 开发者
深入探究Android中的自定义View组件开发
【4月更文挑战第3天】 在现代Android应用程序的开发过程中,创建具有独特功能和高度定制化的用户界面是一个常见需求。为此,理解并掌握自定义View组件的开发成为了开发者必备的技能之一。本文将深入探讨如何在Android中创建自定义View,从基础的绘制原理到事件处理机制,再到性能优化技巧,旨在为读者提供一个全面的技术视角,并通过实例代码演示如何实现一个功能丰富、响应迅速的自定义View组件。
|
1月前
|
XML Java Android开发
Android实现自定义进度条(源码+解析)
Android实现自定义进度条(源码+解析)
57 1
|
5月前
|
XML Android开发 数据安全/隐私保护
Android 自定义开源库 EasyView
Android 自定义开源库 EasyView
|
12天前
|
Android开发 芯片
Android源代码定制:移除无用lunch|新建lunch|自定义customize.mk
Android源代码定制:移除无用lunch|新建lunch|自定义customize.mk
25 3
|
12天前
|
移动开发 Java Unix
Android系统 自动加载自定义JAR文件
Android系统 自动加载自定义JAR文件
33 1
|
12天前
|
Shell Android开发 开发者
Android系统 自定义动态修改init.custom.rc
Android系统 自定义动态修改init.custom.rc
26 0
|
12天前
|
存储 安全 Android开发
Android系统 自定义系统和应用权限
Android系统 自定义系统和应用权限
23 0