杨子刚的博客


Android控件自定义属性

2013-12-08

在Android我们可以为自定义的控件定义自己的属性,这些属性的类型有如下几个:

这些类型具体如何定义,大家可以参考Android的原文件attrs.xml,根据这个文件,我整理了一下,仅供查阅方便。

文件res/values/attrs.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <resources>
 3     <declare-styleable name="FragmentOne">
 4         <attr name="stringReferenceValue" format="reference" />
 5         <attr name="colorReferenceValue" format="reference|color" />
 6         <attr name="drawableReferenceValue" format="reference" />
 7         <attr name="referenceValue" format="reference" />          
 8         <attr name="stringValue" format="string" />
 9         <attr name="colorValue" format="color" />
10         <attr name="dimensionValue" format="dimension" />
11         <attr name="booleanValue" format="boolean" />
12         <attr name="integerValue" format="integer" />
13         <attr name="floatValue" format="float" />
14         <attr name="fractionValue" format="fraction" />
15         <attr name="enumValue">
16             <enum name="vertical" value="0" />
17             <enum name="horizontal" value="1" />
18         </attr>
19         <attr name="flagValue">
20             <flag name="none" value="0x0" />
21             <flag name="bold" value="0x1" />
22             <flag name="italic" value="0x2" />
23             <flag name="all" value="0x3" />
24         </attr>
25     </declare-styleable>
26 </resources>

其中flag类型在赋值时可以使用|符号,即最后的取值为若干个值按位或得到的值,本例中如果我们设置falgValue="bold|italic",则flagValue的值为0x3。

获取通过xml设定的值: * 对于一个自定义的控件来说(一般继承自View),可以通过在构造函数中来获取这些自定义的属性值,它们分别是:public View (Context context, AttributeSet attrs)public View (Context context, AttributeSet attrs, int defStyleAttr) * 对于一个fragment来说,可以在函数public void onInflate (Activity activity, AttributeSet attrs, Bundle savedInstanceState)中获取这些值。

在自定义控件中获取(CustomButton.java):

package com.ingphone.hellocustomview;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.Button;

public class CustomButton extends Button {

    public CustomButton(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray ta = context.obtainStyledAttributes(attrs,
                R.styleable.CustomButton, 0, 0);
        String extraText = ta.getString(R.styleable.CustomButton_extraText);
        Log.i("HelloCustomView", extraText);
        ta.recycle();
    }

}

在fragment中获取(FragmentOne.java)

public void onInflate(Activity activity, AttributeSet attrs,
            Bundle savedInstanceState) {
        super.onInflate(activity, attrs, savedInstanceState);
        Log.v("HelloFragmentArguments", "onInflate called");

        TypedArray a = activity.obtainStyledAttributes(attrs,
                R.styleable.FragmentOne);
        
        mStringReferenceValue = a.getResourceId(R.styleable.FragmentOne_stringReferenceValue, 0);
        mColorReferenceValue = a.getResourceId(R.styleable.FragmentOne_colorReferenceValue, 0);
        mDrawableReferenceValue = a.getResourceId(R.styleable.FragmentOne_drawableReferenceValue, 0);
        
        mReferenceValue = a.getResourceId(R.styleable.FragmentOne_referenceValue, 0);
        mStringValue = a.getString(R.styleable.FragmentOne_stringValue);
        mColorValue = a.getColor(R.styleable.FragmentOne_colorValue, 0);
        mDimensionValue = a.getDimension(R.styleable.FragmentOne_dimensionValue, 0.0f);
        mBooleanValue = a.getBoolean(R.styleable.FragmentOne_booleanValue, false);
        mIntegerValue = a.getInt(R.styleable.FragmentOne_integerValue, 0);
        mFloatValue = a.getFloat(R.styleable.FragmentOne_floatValue, 0.0f);
        mFractionValue = a.getFraction(R.styleable.FragmentOne_fractionValue, 1, 1, 0.0f);
        mEnumValue = a.getInt(R.styleable.FragmentOne_enumValue, 0);
        mFlagValue = a.getInt(R.styleable.FragmentOne_flagValue, 0);
        
        Log.i("HelloFragmentAttributes", "\n==========\n" +
        "attributes are:\n" +
                "mReferenceValue\t"    + mReferenceValue     + "\n" +
                "mStringValue:\t"      + mStringValue        + "\n" +
                "mColorValue:\t"       + mColorValue     + "\n" +
                "mDimensionValue:\t"   + mDimensionValue + "\n" +
                "mBooleanValue:\t"     + mBooleanValue       + "\n" +
                "mIntegerValue:\t"     + mIntegerValue       + "\n" +
                "mFloatValue:\t"       + mFloatValue     + "\n" +
                "mFractionValue:\t"        + mFractionValue  + "\n" +
                "mEnumValue:\t"            + mEnumValue      + "\n" +
                "mFlagValue:\t"            + mFlagValue      + "\n" +
                "==========\n"
        );

        a.recycle();
    }