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

目录
相关文章
|
17天前
|
XML Java Android开发
Android实现自定义进度条(源码+解析)
Android实现自定义进度条(源码+解析)
49 1
|
4月前
|
XML Android开发 数据安全/隐私保护
Android 自定义开源库 EasyView
Android 自定义开源库 EasyView
|
4月前
|
XML Java Android开发
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
40 1
|
7月前
|
API Android开发
Android 自定义最大宽度,高度, 宽高比例 Layout
Android 自定义最大宽度,高度, 宽高比例 Layout
|
4月前
|
XML 搜索推荐 Java
Android App开发之自定义图形中位图与图形互转、剪裁图形内部区域、给图形添加部件的讲解及实战(附源码 简单易懂)
Android App开发之自定义图形中位图与图形互转、剪裁图形内部区域、给图形添加部件的讲解及实战(附源码 简单易懂)
33 0
|
4月前
|
XML 前端开发 Java
Android Studio App自定义控件中自定义视图的绘制讲解及实战(附源码 包括自定义绘制各种图形)
Android Studio App自定义控件中自定义视图的绘制讲解及实战(附源码 包括自定义绘制各种图形)
34 1
|
4月前
|
XML API Android开发
Android 自定义View 之 圆环进度条
Android 自定义View 之 圆环进度条
|
21天前
|
Android开发
Android 开发 pickerview 自定义选择器
Android 开发 pickerview 自定义选择器
12 0
|
7月前
|
XML Android开发 数据格式
自定义Android titleBar
自定义Android titleBar
26 0
|
8月前
|
Android开发
Android 使用ViewPager和自定义PagerAdapter实现轮播图效果
Android 使用ViewPager和自定义PagerAdapter实现轮播图效果
70 0