一、在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();


<declare-styleable name="ToolBar"></declare-styleable>包围所有属性:

1\ 其中name为该属性集的名字,主要用途是标 识该属性集
2\ "R.styleable.ToolBar_buttonNum",在每个属性前面都加了"ToolBar_"

[string , integer , dimension , reference , color , enum]

转自:
http://linapex.blog.163.com/blog/static/189237516201251553056418/

更多相关文章

  1. 一款常用的 Squid 日志分析工具
  2. GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
  3. RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
  4. Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
  5. Android(安卓)Studio 手机运行时中文乱码
  6. Android(安卓)Gradle 多渠道打包
  7. android adb命令打印logcat日志 到指定目录
  8. Android:宏控属性
  9. 利用Android(安卓)Studio将Protobuf文件生成Java文件

随机推荐

  1. Android入门教程(二)之------环境搭建
  2. android EditText中的inputType
  3. 安卓巴士总结了近百个Android优秀开源项
  4. 简单 4部 完成 android 二维码扫一扫功能
  5. Android中扫描多媒体文件操作详解
  6. Android的源代码结构
  7. Android(安卓)逆向apk的配置文件
  8. android重要包的描述
  9. Android(安卓)- 文件读写操作 总结
  10. Android开机动画bootanimation.zip