Currently the best documentation is the source. You can take a look at ithere (attrs.xml).

You can define attributes in the top<resources>element or inside of a<declare-styleable>element. If I'm going to use an attr in more than one place I put it in the root element. Note, all attributes share the same global namespace. That means that even if you create a new attribute inside of a<declare-styleable>element it can be used outside of it and you cannot create another attribute with the same name of a different type.

An<attr>element has two xml attributesnameandformat.namelets you call it something and this is how you end up referring to it in code, e.g.,R.attr.my_attribute. Theformatattribute can have different values depending on the 'type' of attribute you want.

  • reference - if it references another resource id (e.g, "@color/my_color", "@layout/my_layout")
  • color
  • boolean
  • dimension
  • float
  • integer
  • string
  • fraction
  • enum - normally implicitly defined
  • flag - normally implicitly defined

You can set the format to multiple types by using|, e.g.,format="reference|color".

enumattributes can be defined as follows:

<attr name="my_enum_attr">  <enum name="value1" value="1" />  <enum name="value2" value="2" /> </attr> 

flagattributes are similar except the values need to be defined so they can be bit ored together:

<attr name="my_flag_attr">  <flag name="fuzzy" value="0x01" />  <flag name="cold" value="0x02" /> </attr> 

In addition to attributes there is the<declare-styleable>element. This allows you to define attributes a custom view can use. You do this by specifying an<attr>element, if it was previously defined you do not specify theformat. If you wish to reuse an android attr, for example, android:gravity, then you can do that in thename, as follows.

An example of a custom view<declare-styleable>:

<declare-styleable name="MyCustomView">  <attr name="my_custom_attribute" />  <attr name="android:gravity" /> </declare-styleable> 

When defining your custom attributes in XML on your custom view you need to do a few things. First, declare a namespace to find your attributes. You do this on the top layout element. Normally there is thexmlns:android="http://schemas.android.com/apk/res/android"well you must also doxmlns:whatever="http://schemas.android.com/apk/res/org.example.mypackage". Theorg.example.mypackageis the root package as it is defined in yourAndroidManifest.xml.

Example:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:whatever="http://schemas.android.com/apk/res/org.example.mypackage"  android:orientation="vertical"  android:layout_width="fill_parent"  android:layout_height="fill_parent">   <org.example.mypackage.MyCustomView    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:gravity="center"    whatever:my_custom_attribute="Hello, world!" /> </LinearLayout> 

Finally, to access that custom attribute you normally do so in the constructor of your custom view as follows.

public MyCustomView(Context context, AttributeSet attrs, int defStyle) {  super(context, attrs, defStyle);  TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView, defStyle, 0);  String str = a.getString(R.styleable.MyCustomView_my_custom_attribute);  //do something with str  a.recycle(); } 

The end. :)

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. 详解sqlserver查询表索引
  2. 在SQL SERVER中导致索引查找变成索引扫描
  3. SQL Server数据复制到的Access两步走
  4. 一步步教你建立SQL数据库的表分区
  5. 非常好用的sql语句(日常整理)
  6. 安装完成后如何找回SQL Server实例安装时
  7. SQL Server2014 哈希索引原理详解
  8. 恢复sql server 2000误删数据的解决办法
  9. SQLSERVER SQL性能优化技巧
  10. SQL字段拆分优化