您的位置:首页 > 移动开发 > Android开发

Android 自定义 declare-styleable 的format

2015-12-25 16:04 507 查看
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来释放元素
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: