Android定制控件

参考了网上资料,搞清三个问题:

1. 如何定制控件;2. 如何为定制控件添加新的属性;3. 如何在XML里应用。控件的定制主要有两种形式,一种是从类View派生,就像TextView等控件一样,http://developer.android.com/guide/topics/ui/custom-components.html有比较详细的解释。另外一种就是在改造现有的控件,或者由现有的多个控件组装成一个新的控件,添加新的方法或者属性。

下面的例子是封装一个带有icon的按钮,只是一个形式,不一定有按钮的功能。只是为了解释上面三个问题。

1. 先定义一个layout文件,包含了一个现实图标的ImageView和现实文字的TextView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <ImageView
  	android:id="@+id/iv" 
  	android:layout_width="wrap_content"
  	android:layout_height="wrap_content"
  	android:src="@drawable/ok"
  	android:paddingTop="5dip"
  	android:paddingBottom="5dip"
  	android:paddingLeft="40dip"
  	android:layout_gravity="center_vertical"
  />
  <TextView 
  	android:id="@+id/tv"
  	android:layout_width="wrap_content"
  	android:layout_height="wrap_content"
  	android:text="OK"
  	android:textColor="#FFFFFFFF"
  	android:layout_marginLeft="8dip"
  	android:layout_gravity="center_vertical"
  />  
</LinearLayout>

2. 定义一个类MyButton,封装我们需要的控件

package com.example;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyButton extends LinearLayout {
	private ImageView mIv;
	private TextView mTv;
	public MyButton(Context context) {
		this(context,null);
		// TODO Auto-generated constructor stub
	}
	public MyButton(Context context, AttributeSet attrs)
	{
		super(context,attrs);
		LayoutInflater.from(context).inflate(R.layout.mybutton, this, true);
		mIv = (ImageView)findViewById(R.id.iv);
		mTv = (TextView)findViewById(R.id.tv);
		
                TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyButton);
		String text = a.getString(R.styleable.MyButton_text);
		if(text != null)
			setTextViewText(text);
		Drawable dr= a.getDrawable(R.styleable.MyButton_icon);
		mIv.setImageDrawable(dr);
		
		a.recycle();
	}
	public void setImageResource(int resId)
	{
		mIv.setImageResource(resId);
	}
	public void setTextViewText(String text)
	{
		mTv.setText(text);
	}
}
在MyButton的构造函数里,将mybutton.xml转换成了view的可视图,后面的代码取得两个新的属性icon和text;最后两个函数用来设置icon给ImageView,text给TextView.

3. 接下来的问题是如何声明两个新属性

在value下面创建attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<declare-styleable name="MyButton">
		<attr name="icon" format="integer"/>
		<attr name="text" format="string"/>
	</declare-styleable>    
</resources>

declare-styleable声明了两个新属性,一个是图片icon,格式是integer,另外是text,格式是string. 这个文件会自动生成在R里面,通过R就可以访问。

4. 那如何使用这个新的控件呢?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:fsms="http://schemas.android.com/apk/res/com.example"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
 	<RelativeLayout 
 		android:layout_width="wrap_content"
 		android:layout_height="wrap_content"
 		android:orientation = "horizontal"
 		android:layout_gravity="bottom"
 	>
	 	<com.example.MyButton
	 		android:id="@+id/bt_confirm"
	 		android:layout_width="wrap_content"
	 		android:layout_height="wrap_content"
	 		android:layout_alignParentBottom="true"
	 		android:background="@drawable/button_default"
	 		android:clickable="true"
	 		android:focusable="true"
                        fsms:icon="@drawable/ok"
	 		fsms:text="Yes"
	 	/>
	 	<com.example.MyButton
	 		android:id="@+id/bt_cancel"
	 		android:layout_width="wrap_content"
	 		android:layout_height="wrap_content"
	 		android:layout_alignParentBottom="true"
	 		android:layout_toRightOf="@+id/bt_confirm"
	 		android:background="@drawable/button_default"
	 		android:clickable="true"
	 		android:focusable="true"
                        fsms:icon="@drawable/cancel"
	 		fsms:text="No"
	 	
	 	/>  	 	  	
 	</RelativeLayout> 
   
</LinearLayout>

要在xml里面使用新的属性,必须先声明名字空间,http://schemas.android.com/apk/res这段是一样的,关键是后面的com.example,这是包名。

<com.example.MyButton/>是新的控件的名字。fsms是新声明的名字空间。接下来就向使用android:id一样,可以使用fsms:icon和fsms:text属性了。

结果图如下:


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值