Android自定义控件 使用自定义属性

说明:下面的例子使用了AndroidAnnotation,并非标准的开发模式,但是几行重要代码都是相同的。
这个例子是一个按钮,外层是LinearLayout,垂直布局,里面是一个ImageView,一个TextView,需要添加自定义的属性,以支持在Xml中写入ImageView的图片和TextView的文字。
先声明属性:
添加values/attrs.xml:


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MainTabButton">
        <attr name="src" format="reference"></attr>
        <attr name="text" format="string|reference"></attr>
    </declare-styleable>
</resources>

声明了两个属性,一个src,限制值为引用,一个text,限制值为字符串或引用.
format其他可能的值:
reference:引用其他资源
color:颜色
boolean:布尔
dimension:尺寸
float:浮点
integer:整数
string:字符串
fraction:百分比
enum:枚举,用法:


       <attr name="language">
            <enum name="English" value="1"/>
        </attr>

flag:位或运算,用法


<attr name="windowSoftInputMode">
    	<flag name="stateUnspecified" value="1" />
    	<flag name = "adjustNothing" value = "0x30" />
        </attr>

<activity android:windowSoftInputMode="stateUnspecified | adjustNothing">

添加完属性后,写相应的类:
MainTabButton.java:


package com.pocketdigi.components;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.EViewGroup;
import com.googlecode.androidannotations.annotations.ViewById;
import com.pocketdigi.dayday.R;

@EViewGroup(R.layout.component_button)
public class MainTabButton extends LinearLayout {
	@ViewById
	ImageView imageView;
	@ViewById
	TextView textView;
	int imageId;
	String text;
	public MainTabButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.MainTabButton);
		//读取XML中设置的属性值
		imageId=a.getResourceId(R.styleable.MainTabButton_src, R.drawable.tab_radio_channel_normal);
		text=a.getString(R.styleable.MainTabButton_text);
		a.recycle();
	}
	//视图载入后,再设置两个控件
	@AfterViews
	public void afterViews()
	{
		setImage(imageId);
		setText(text);
	}

	public void setImage(int resId) {
		imageView.setImageResource(resId);
	}

	public void setText(String text) {
		textView.setText(text);
	}
	//选中方法,如果选中,显示另一张图片,并改变文字颜色
	public void selected(boolean isSelected)
	{
		if(isSelected)
		{
			switch(imageId)
			{
			case R.drawable.tab_radio_home_normal:
				setImage(R.drawable.tab_radio_home_selected);
				textView.setTextColor(getResources().getColor(R.color.tab_text_selected));
				break;
			case R.drawable.tab_radio_channel_normal:
				setImage(R.drawable.tab_radio_channel_selected);
				textView.setTextColor(getResources().getColor(R.color.tab_text_selected));
				break;
			case R.drawable.tab_radio_download_normal:
				setImage(R.drawable.tab_radio_download_selected);
				textView.setTextColor(getResources().getColor(R.color.tab_text_selected));
				break;
			case R.drawable.tab_radio_search_normal:
				setImage(R.drawable.tab_radio_search_selected);
				textView.setTextColor(getResources().getColor(R.color.tab_text_selected));
				break;
			case R.drawable.tab_radio_setting_normal:
				setImage(R.drawable.tab_radio_setting_selected);
				textView.setTextColor(getResources().getColor(R.color.tab_text_selected_blue));
				break;
			}
		}
	}

}

XML中使用:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:dayday="http://schemas.android.com/apk/res/com.pocketdigi.dayday"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:layout_gravity="center"
    android:background="@drawable/tab_bg"
    android:gravity="center"
    android:orientation="horizontal"
    android:weightSum="5" >

    <com.pocketdigi.components.MainTabButton_
        android:id="@+id/home"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:paddingLeft="10dp"
        dayday:src="@drawable/tab_radio_home_normal"
        dayday:text="首页" >
    </com.pocketdigi.components.MainTabButton_>
</LinearLayout>

© 2012, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接! 应用开发笔记