http://www.cnblogs.com/jisheng/archive/2013/01/10/2854891.html

在使用过程中,

1 TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ContactListItemView);
2 mPreferredHeight = a.getDimensionPixelSize(
3 R.styleable.ContactListItemView_list_item_height, 0);
4 mActivatedBackgroundDrawable = a.getDrawable(
5 R.styleable.ContactListItemView_activated_background);
6 mHorizontalDividerDrawable = a.getDrawable(
7 R.styleable.ContactListItemView_list_item_divider);

发现mHorizontalDividerDrawable一直为空。

查找到在attrs.xml里定义了

<declare-styleable name="ContactListItemView">

....

<attr name="list_item_divider" format="reference"/>

...
</declare-styleable>

最后发现该引用出现在ActivityTheme里面

<style name="PeopleTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">

.......
<item name="list_item_divider">?android:attr/listDivider</item> .......
</style>

相关资料如下

做Android布局是件很享受的事,这得益于他良好的xml方式。使用xml可以快速有效的为软件定义界面。可是有时候我们总感觉官方定义的一些
基本组件不够用,自定义组件就不可避免了。那么如何才能做到像官方提供的那些组件一样用xml来定义他的属性呢?现在我们就来讨论一下他的用法。

一、在res/values文件下定义一个attrs.xml文件,代码如下:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="ToolBar"> 
        <attr name="buttonNum" format="integer"/> 
        <attr name="itemBackground" format="reference|color"/> 
    </declare-styleable> 
</resources>

二、在布局xml中如下使用该属性:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:toolbar="http://schemas.android.com/apk/res/cn.zzm.toolbar" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <cn.zzm.toolbar.ToolBar android:id="@+id/gridview_toolbar" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true" 
        android:background="@drawable/control_bar" 
        android:gravity="center" 
        toolbar:buttonNum="5" 
        toolbar:itemBackground="@drawable/control_bar_item_bg"/> 
</RelativeLayout>

三、在自定义组件中,可以如下获得xml中定义的值:

TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.ToolBar); 
buttonNum = a.getInt(R.styleable.ToolBar_buttonNum, 5); 
itemBg = a.getResourceId(R.styleable.ToolBar_itemBackground, -1);

a.recycle();

就这么简单的三步,即可完成对自定义属性的使用。

*********************************************************************

好了,基本用法已经讲完了,现在来看看一些注意点和知识点吧。

首先来看看attrs.xml文件。

该文件是定义属性名和格式的地方,需要用<declare-styleable
name="ToolBar"></declare-styleable>包围所有属性。其中name为该属性集的名字,主要用途是标
识该属性集。那在什么地方会用到呢?主要是在第三步。看到没?在获取某属性标识时,用
到"R.styleable.ToolBar_buttonNum",很显然,他在每个属性前面都加了"ToolBar_"。

在来看看各种属性都有些什么类型吧:string , integer , dimension , reference , color , enum......

前面几种的声明方式都是一致的,例如:<attr name="buttonNum" format="integer"/>。 
只有enum是不同的,用法举例:

<attr name="testEnum"> 
    <enum name="fill_parent" value="-1"/> 
    <enum name="wrap_content" value="-2"/> 
</attr>

如果该属性可同时传两种不同的属性,则可以用“|”分割开即可。

让我们再来看看布局xml中需要注意的事项。

首先得声明一下:xmlns:toolbar=http://schemas.android.com/apk/res/cn.zzm.toolbar 
注意,“toolbar”可以换成其他的任何名字,后面的url地址必须最后一部分必须用上自定义组件的包名。自定义属性了,在属性名前加上“toolbar”即可。

最后来看看java代码中的注意事项。

在自定义组件的构造函数中,用

TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.ToolBar);

来获得对属性集的引用,然后就可以用“a”的各种方法来获取相应的属性值了。这里需要注意的是,如果使用的方法和获取值的类型不对的话,则会返回默
认值。因此,如果一个属性是带两个及以上不用类型的属性,需要做多次判断,知道读取完毕后才能判断应该赋予何值。当然,在取完值的时候别忘了回收资源哦!

自定义属性数据类型简介:

一、reference:参考指定Theme中资源ID。

1.定义:

1
2
3
    <declare-styleable name="My">
<attr name="label" format="reference" >
</declare-styleable>

2.使用:

1
    <Buttonzkx:label="@string/label" >

二、Color:颜色

1.定义:

1
2
3
    <declare-styleable name="My">
<attr name="textColor" format="color" />
</declare-styleable>

2.使用:

1
    <Button zkx:textColor="#ff0000"/>

三、boolean:布尔值

1.定义:

1
2
3
    <declare-styleable name="My">
<attr name="isVisible" format="boolean" />
</declare-styleable>

2.使用:

1
    <Button zkx:isVisible="false"/>

四、dimension:尺寸值

1.定义:

1
2
3
    <declare-styleable name="My">
<attr name="myWidth" format="dimension" />
</declare-styleable>

2.使用:

1
    <Button zkx:myWidth="100dip"/>

五、float:浮点型

1.定义:

1
2
3
    <declare-styleable name="My">
<attr name="fromAlpha" format="float" />
</declare-styleable>

2.使用:

1
    <alpha zkx:fromAlpha="0.3"/>

六、integer:整型

1.定义:

1
2
3
    <declare-styleable name="My">
<attr name="frameDuration" format="integer" />
</declare-styleable>

2.使用:

1
    <animated-rotate zkx:framesCount="22"/>

七、string:字符串

1.定义:

1
2
3
    <declare-styleable name="My">
<attr name="Name" format="string" />
</declare-styleable>

2.使用:

1
    <rotate zkx:pivotX="200%"/>

八、fraction:百分数

1.定义:

1
2
3
    <declare-styleable name="My">
<attr name="pivotX" format="fraction" />
</declare-styleable>

2.使用:

1
    <rotate zkx:Name="My name is zhang kun xiang"/>

九、enum:枚举

1.定义:

1
2
3
4
5
    <declare-styleable name="My">
<attr name="language">
<enum name="English" value="1"/>
</attr>
</declare-styleable>

2.使用:

1
    <Button zkx:language="English"/>

十、flag:位或运算

1.定义:

1
2
3
4
5
6
    <declare-styleable name="My">
<attr name="windowSoftInputMode">
<flag name="stateUnspecified" value="1" />
<flag name = "adjustNothing" value = "0x30" />
</attr>
</declare-styleable>

2.使用:

1
    <activity android:windowSoftInputMode="stateUnspecified | adjustNothing">

属性定义时可以指定多种类型值:

1
2
3
    <declare-styleable name = "名称">
<attr name="background" format="reference|color" />
</declare-styleable>

使用:

1
    <ImageView android:background = "@drawable/图片ID|#00FF00"/>

declare-styleable:自定义控件的属性的更多相关文章

  1. C# 如何定义让PropertyGrid控件显示[...]按钮,并且点击后以下拉框形式显示自定义控件编辑属性值

    关于PropertyGrid控件的详细用法请参考文献: 1.C# PropertyGrid控件应用心得 2.C#自定义PropertyGrid属性 首先定义一个要在下拉框显示的控件: using Sy ...

  2. Android中自定义控件TextSize属性问题

    本文主要说明一个自定义控件添加TextSize属性的坑,刚刚从坑里面爬出来,写个随笔,记录一下: *************************************************** ...

  3. Android - 自定义控件和属性(attr和TypedArray)

    http://blog.csdn.net/zjh_1110120/article/details/50976027 1.attr format 取值类型 以ShapeView 为例 <decla ...

  4. attrs.xml中declare-styleable 详解(用于自定义控件的属性)

    1. 框架定义: <declare-styleable name = "名称"> <attr name = "……" format = &qu ...

  5. Android 中自定义控件和属性(attr.xml,declare-styleable,TypedArray)的方法和使用

    一. 在res/values 文件下定义一个attrs.xml 文件.代码如下: <?xml version="1.0" encoding="utf-8" ...

  6. Android开发学习笔记-自定义控件的属性

    若想让自定义控件变得更加方便灵活,则就需要对控件进行定义属性,使其用起来更方便. 下面是自定义控件属性的方法 1.添加attrs.xml,内容格式样式可以参考sdk\platforms\android ...

  7. C# windform自定义控件的属性小知识

    word中的加粗变斜之类的一直让我以为是button,直到我接触了自定义控件,才发现实现这种机能最好的是CheckBox,然后我们在做一个系统的时候,这种控件有可能要用好多次,总不能在用一次的时候,就 ...

  8. 自定义控件设置属性并实时展现并预览在xib中

    关键字: // @IBDesignable:实时看到xib设置后的效果 // @IBInspectable:给xib提供设置属性,可以xib中看到此属性 场景: 自定义一个UITextField,并提 ...

  9. [android] 手机卫士自定义控件的属性

    上一节完成的自定义组合控件,灵活性不够,控件的显示信息上,仿照系统属性,自定义自己的属性 上一节组合控件SettingItemView中有三个控件,分别是TextView大标题,TextView描述, ...

随机推荐

  1. MySQL源码之Thread cache

    MySQL server为每一个connection建立一个thread为其服务,虽然thread create比着fork process代价高,单高并发的情况下,也不可忽略. 所以增加了Threa ...

  2. WordPress Lazy SEO插件lazyseo.php脚本任意文件上传漏洞

    漏洞名称: WordPress Lazy SEO插件lazyseo.php脚本任意文件上传漏洞 CNNVD编号: CNNVD-201309-446 发布时间: 2013-09-26 更新时间: 201 ...

  3. QT更改程序图标

    方法只要几个步骤就好了,如下: 1.准备好一个ico格式的图标文件,例如demo.ico 2.创建一个rc文件, 例如demo.rc. 并copy下面代码: // Generated by ResEd ...

  4. [App]华为P6设置与Xamarin Studio连通测试

    使用模拟器进行调试十分麻烦,而且速度很慢,手头上有手机一台正好做测试机器,不过一直无法连通电脑. 百度了一番才知道,首先要在连接时候选择: PC 助手(HiSuite) 这样会加载一个类似驱动盘的资源 ...

  5. leetcode 二分查找

    https://oj.leetcode.com/problems/search-for-a-range/就是一个二分查找,没事练练手 public class Solution { public in ...

  6. strcpy,memcpy,内存块重叠

    前段时间准备面试,看了一些库函数的实现,在看到memcpy时,发现有处理source和destination所指内存有重叠的情况,而strcpy没有,特别模仿库函数写了这个函数,并进行了测试.以下是具 ...

  7. Cogs 309. [USACO 3.2] 香甜的黄油 dijkstra,堆,最短路,floyd

    题目:http://cojs.tk/cogs/problem/problem.php?pid=309 309. [USACO 3.2] 香甜的黄油 ★★   输入文件:butter.in   输出文件 ...

  8. fork和缓冲区

    fork在面试中经常被问到,在这里复习一下. frok创建子进程,父子进程共享.text段,子进程获得父进程数据段.堆和栈的副本,由于在fork之后经常跟随者exec,所以很多实现并不执行父进程数据段 ...

  9. ln命令

    图形化界面创建ln 命令行界面创建ln 命令行界面创建ln 后续,更新

  10. radix树

    今天在学Linux内核页高速缓存时,学到了一种新的数据结构radix树(基数),经过分析,感觉此数据结构有点鸡肋,有可能是我理解不到位吧. 先来张图,给大家以直观的印象 当有一个key-value型的 ...