Android开发之自定义属性(Define Custom Attributes)

为了在你用户界面上添加Android自带的View,你可以在一个XML元素中指定这个View,并通过它相应的元素属性控制该View的外观和行为。

写得好的自定义View一样可以通过XML添加和设置样式,为了能够为您的自定义View添加这些行为,你必须:

1. <declare-styleable>资源元素中为你的View自定义属性;

2.在你的XML布局中为这个属性指定相应的值;

3.在程序运行时取回属性值;

4.在你的View中使用你所取回的属性值;

本节讨论如何定义自定义属性并指定他们的值。下一节则是有关在运行时取得并使用这些值。

为了自定义属性,添加<declare-styleable>资源到你的项目中,一般这些资源通常放在 res/values/attrs.xml文件中

这里有一个attrs.xml文件的例子

<resources>;
   <declare-styleable name="PieChart">
       <attr name="showText" format="boolean" />
       <attr name="labelPosition" format="enum">
           <enum name="left" value="0"/>
           <enum name="right" value="1"/>
       </attr>
   </declare-styleable>
</resources>
这些代码声明了两个自定义属性:"showText"和"labelPosition",他们属于一个叫做PieChart的样式实体。按照惯例,样式实体的名字是和声明的自定义view类名是相同的。尽管遵循这个惯例不是绝对必要的,但很多有名的代码编写者都基于这个命名惯例来提供声明。
一旦您定义了自定义属性,您可以在布局XML文件中像内建属性一样使用它们。唯一的不同是,您的自定义属性属于不同的命名空间。它们属于
http://schemas.android.com/apk/res/[your package name] 以取代默认的http://schemas.android.com/apk/res/android 命名空间。下面例子显示如何为PieChart使用这些定义过的属性:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:custom="http://schemas.android.com/apk/res/com.example.customviews">
 <com.example.customviews.charting.PieChart
     custom:showText="true"
     custom:labelPosition="left" />
</LinearLayout>
为了避免重复使用长的命名空间URI,本例使用了一个xmlns指示符。这个指示指定了别名custom为命名空间http://schemas.android.com/apk/res/com.example.customviews 您可以为您的命名空间选择任意的别名。

注意您用来向布局中添加自定义view的XML标签的名字。这是自定义view类的完全表述。如果您的view内是一个内部类,您必须使用外部类的名字进一步限定它。例如,PieChart类有一个叫做PieView的内部类。为了使用这个类中的自定义属性,您必须使用标签com.example.customviews.charting.PieChart$PieView

应用自定义属性:

当view从XML布局中创建了之后,XML标签中所有的属性都从资源包中读取出来并作为一个AttributeSet传递给view的构造函数。尽管从AttributeSet中直接读取值是可以的,但是这样做有一些缺点:
带有值的资源引用没有进行处理样式并没有得到允许取而代之的是,将AttributeSet传递给obtainStyledAttributes()方法。这个方法传回了一个TypedArray数组,包含了已经解除引用和样式化的值。
为了时能能够更容易的调用obtainStyledAttributes()方法,Android资源编译器做了大量的工作。res文件夹中的每个
<declare-styleable>资源,生成的R.java都定义了一个属性ID的数组以及一套定义了指向数组中的每一个属性的常量。您可以使用预定义的常量从TypedArray中读取属性。下例是PieChart类是如何读取这些属性的:

public PieChart(Context ctx, AttributeSet attrs) {
   super(ctx, attrs);
   TypedArray a = context.getTheme().obtainStyledAttributes(
        attrs,
        R.styleable.PieChart,
        0, 0);

   try {
       mShowText = a.getBoolean(R.styleable.PieChart_showText, false);
       mTextPos = a.getInteger(R.styleable.PieChart_labelPosition, 0);
   } finally {
       a.recycle();
   }
}

注意:TypedArray对象是一个共享的资源,使用完毕必须回收它。

下面是系统layout_width自定义属性

      <attr name="layout_width" format="dimension">
            <!-- The view should be as big as its parent (minus padding).
                 This constant is deprecated starting from API Level 8 and
                 is replaced by {@code match_parent}. -->
            <enum name="fill_parent" value="-1" />
            <!-- The view should be as big as its parent (minus padding).
                 Introduced in API Level 8. -->
            <enum name="match_parent" value="-1" />
            <!-- The view should be only big enough to enclose its content (plus padding). -->
            <enum name="wrap_content" value="-2" />
      </attr>


扩展阅读:

Android中自定义属性的格式详解




发布了61 篇原创文章 · 获赞 380 · 访问量 58万+

猜你喜欢

转载自blog.csdn.net/manymore13/article/details/8755068