Android--自定义控件的xml属性

来源:互联网 发布:网络高清机顶盒哪个好 编辑:程序博客网 时间:2024/06/05 23:55


1. 定义一些自定义属性

    建立一个属性xml文件: values/attrs.xml, 内容如下:

   

<?xml version="1.0" encoding="utf-8"?><resources>    <!-- 图片和文字之间的位置关系  使用enum声明的属性-->    <attr name="relation">        <enum name="icon_left" value="0" />        <enum name="icon_right" value="1" />        <enum name="icon_above" value="2" />        <enum name="icon_below" value="3" />    </attr>    <declare-styleable name="IconText">        <attr name="relation" />        <attr name="icon" format="reference" />        <attr name="text" format="string" />        <attr name="text_size" format="dimension" />        <attr name="text_color" format="integer" />        <attr name="space" format="dimension" />    </declare-styleable></resources><!--
属性relation有4种可选值:icon_left, icon_right, icon_above,icon_below.属性icon的可选值为引用: 例如:"@/drawbable/icon".属性text的可选值为string, 例如: "Hello world", 也可是string的引用"@string/hello".属性text_size的可选值为尺寸大小,例如:20sp、18dip、20px等.属性text_color的可选值为整数,例如:"0xfffffff", 也可以是color的引用"@color/white". -->


该文件是定义属性名和格式的地方,需要用<declare-styleable name="IconText"></declare-styleable>包围所有属性。

其中name为该属性集的名字,主要用途是标识该属性集。

在控件的构造方法中获取自定义属性集合的时候,会使用该name。

如:TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.IconText);

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


2. 定义一个能够处理这些属性值的view或者layout类

package com.test_customattributeset;import android.content.Context;import android.content.res.TypedArray;import android.util.AttributeSet;import android.util.Log;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;public class IconTextView extends LinearLayout {private final static String TAG = IconTextView.class.getSimpleName();private final int ICON_LEFT = 0;private final int ICON_RIGHT = 1;private final int ICON_ABOVE = 2;private final int ICON_BELOW = 3;private TextView mTextView;private ImageView mImageView;private int mRelation = ICON_LEFT;private String mText = "";private int mIconId;private float mTextSize;private int mSpace;public IconTextView(Context context) {super(context);}public IconTextView(Context context, AttributeSet attrs) {super(context, attrs);TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.IconText);mRelation = a.getInt(R.styleable.IconText_relation, ICON_LEFT);Log.d(TAG, "mRelation: " + mRelation);mText = a.getString(R.styleable.IconText_text);Log.d(TAG, "mText: " + mText);mTextSize = a.getDimensionPixelSize(R.styleable.IconText_text_size, 12);Log.d(TAG, "mTextSize: " + mTextSize);mSpace = a.getDimensionPixelSize(R.styleable.IconText_space, 5);Log.d(TAG, "mSpace: " + mSpace);mIconId = a.getResourceId(R.styleable.IconText_icon,R.drawable.ic_launcher);Log.d(TAG, "mIconId: " + mIconId);a.recycle();mTextView = new TextView(context);mTextView.setText(mText);mTextView.setTextSize(mTextSize);mImageView = new ImageView(context);mImageView.setImageResource(mIconId);int left = 0;int top = 0;int right = 0;int bottom = 0;int orientation = HORIZONTAL;int textViewIndex = 0;switch (mRelation) {case ICON_ABOVE:orientation = VERTICAL;bottom = mSpace;textViewIndex = 1;break;case ICON_BELOW:orientation = VERTICAL;top = mSpace;break;case ICON_LEFT:right = mSpace;textViewIndex = 1;break;case ICON_RIGHT:left = mSpace;break;default:break;}this.setOrientation(orientation);this.addView(mImageView);mImageView.setPadding(left, top, right, bottom);this.addView(mTextView, textViewIndex);}}

通过obtainStyledAttributes方法获得对属性集的引用,用“a”的各种方法来获取相应的属性值。这里需要注意的是,如果使用的方法和获取值的类型不对的话,则会返回默认值。

因此,如果一个属性是带两//个及以上不用类型的属性,需要做多次判断,直到读取完毕后才能判断应该赋予何值。当然,在取完值的时候别忘了回收资源。

类IconTextView中只有两个元素,ImageView 和mTextView,通过从xml配置文件中读取属性值来决定icon和text的内容、相对位置及其它属性。


3. 在layout布局文件中使用这个自定布局及其属性

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:icontext="http://schemas.android.com/apk/res/com.test_customattributeset"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <com.test_customattributeset.IconTextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        icontext:icon="@drawable/ic_launcher"        icontext:relation="icon_left"        icontext:text="hi,how are you!"        icontext:text_size="12sp" /></RelativeLayout>


注意点:我们需要在这个布局文件中加入了一个新的命名空间:

xmlns:icontext=http://schemas.android.com/apk/res/com.test_customattributeset

其中“http://schemas.android.com/apk/res/”是固定的,“com.test_customattributeset”是我们工程的包名。

"icontext"可以换成其他的任何名字.

使用如下方式使用自定义的属性:

        icontext:icon="@drawable/ic_launcher"
        icontext:relation="icon_left"
        icontext:text="hi,how are you!"
        icontext:text_size="12sp" />


4. 在Activity中使用该布局

package com.test_customattributeset;import android.os.Bundle;import android.app.Activity;import android.view.Menu;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}


运行效果如下图:



附:Android自定义属性的格式详解
1. reference:参考某一资源ID。
    (1)属性定义:
            <declare-styleable name = "名称">
                   <attr name = "background" format = "reference" />
            </declare-styleable>
    (2)属性使用:
             <ImageView
                     android:layout_width = "42dip"
                     android:layout_height = "42dip"
                     android:background = "@drawable/图片ID"
                     />
2. color:颜色值。
    (1)属性定义:
            <declare-styleable name = "名称">
                   <attr name = "textColor" format = "color" />
            </declare-styleable>
    (2)属性使用:
            <TextView
                     android:layout_width = "42dip"
                     android:layout_height = "42dip"
                     android:textColor = "#00FF00"
                     />
3. boolean:布尔值。
    (1)属性定义:
            <declare-styleable name = "名称">
                   <attr name = "focusable" format = "boolean" />
            </declare-styleable>
    (2)属性使用:
            <Button
                    android:layout_width = "42dip"
                    android:layout_height = "42dip"
                    android:focusable = "true"
                    />
4. dimension:尺寸值。
    (1)属性定义:
            <declare-styleable name = "名称">
                   <attr name = "layout_width" format = "dimension" />
            </declare-styleable>
    (2)属性使用:
            <Button
                    android:layout_width = "42dip"
                    android:layout_height = "42dip"
                    />
5. float:浮点值。
    (1)属性定义:
            <declare-styleable name = "AlphaAnimation">
                   <attr name = "fromAlpha" format = "float" />
                   <attr name = "toAlpha" format = "float" />
            </declare-styleable>
    (2)属性使用:
            <alpha
                   android:fromAlpha = "1.0"
                   android:toAlpha = "0.7"
                   />
6. integer:整型值。
    (1)属性定义:
            <declare-styleable name = "AnimatedRotateDrawable">
                   <attr name = "visible" />
                   <attr name = "frameDuration" format="integer" />
                   <attr name = "framesCount" format="integer" />
                   <attr name = "pivotX" />
                   <attr name = "pivotY" />
                   <attr name = "drawable" />
            </declare-styleable>
    (2)属性使用:
            <animated-rotate
                   xmlns:android = "http://schemas.android.com/apk/res/android
                   android:drawable = "@drawable/图片ID" 
                   android:pivotX = "50%" 
                   android:pivotY = "50%" 
                   android:framesCount = "12" 
                   android:frameDuration = "100"
                   />
7. string:字符串。
    (1)属性定义:
            <declare-styleable name = "MapView">
                   <attr name = "apiKey" format = "string" />
            </declare-styleable>
    (2)属性使用:
            <com.google.android.maps.MapView
                    android:layout_width = "fill_parent"
                    android:layout_height = "fill_parent"
                    android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"
                    />
8. fraction:百分数。
    (1)属性定义:
            <declare-styleable name="RotateDrawable">
                   <attr name = "visible" />
                   <attr name = "fromDegrees" format = "float" />
                   <attr name = "toDegrees" format = "float" />
                   <attr name = "pivotX" format = "fraction" />
                   <attr name = "pivotY" format = "fraction" />
                   <attr name = "drawable" />
            </declare-styleable>
    (2)属性使用:
            <rotate  xmlns:android = "http://schemas.android.com/apk/res/android"
               android:interpolator = "@anim/动画ID"
                 android:fromDegrees = "0"
               android:toDegrees = "360"
                 android:pivotX = "200%"
                 android:pivotY = "300%"
               android:duration = "5000"
                 android:repeatMode = "restart"
                 android:repeatCount = "infinite"
                   />
9. enum:枚举值。
    (1)属性定义:
            <declare-styleable name="名称">
                   <attr name="orientation">
                          <enum name="horizontal" value="0" />
                          <enum name="vertical" value="1" />
                   </attr>           
            </declare-styleable>
    (2)属性使用:
            <LinearLayout
                    xmlns:android = "http://schemas.android.com/apk/res/android"
                    android:orientation = "vertical"
                    android:layout_width = "fill_parent"
                    android:layout_height = "fill_parent"
                    >
            </LinearLayout>
10. flag:位或运算。
     (1)属性定义:
             <declare-styleable name="名称">
                    <attr name="windowSoftInputMode">
                            <flag name = "stateUnspecified" value = "0" />
                            <flag name = "stateUnchanged" value = "1" />
                            <flag name = "stateHidden" value = "2" />
                            <flag name = "stateAlwaysHidden" value = "3" />
                            <flag name = "stateVisible" value = "4" />
                            <flag name = "stateAlwaysVisible" value = "5" />
                            <flag name = "adjustUnspecified" value = "0x00" />
                            <flag name = "adjustResize" value = "0x10" />
                            <flag name = "adjustPan" value = "0x20" />
                            <flag name = "adjustNothing" value = "0x30" />
                     </attr>        
             </declare-styleable>
     (2)属性使用:
            <activity
                   android:name = ".StyleAndThemeActivity"
                   android:label = "@string/app_name"
                   android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">
                   <intent-filter>
                          <action android:name = "android.intent.action.MAIN" />
                          <category android:name = "android.intent.category.LAUNCHER" />
                   </intent-filter>

             </activity>
     注意:
     属性定义时可以指定多种类型值。
    (1)属性定义:
            <declare-styleable name = "名称">
                   <attr name = "background" format = "reference|color" />
            </declare-styleable>
    (2)属性使用:
             <ImageView
                     android:layout_width = "42dip"
                     android:layout_height = "42dip"
                     android:background = "@drawable/图片ID|#00FF00"
                     />




   







1 0
原创粉丝点击