论坛首页 移动开发技术论坛

android自定义控件以及复用控件

浏览 5005 次
精华帖 (0) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-09-29   最后修改:2011-10-30







对于上述几张图片,他们都有一个共同点,布局差不多,我们可以把它封装成一个共同的控件,通过自定义属性来控制子控件的显示与否与内容的不同,背景的不同。

代码:
package com.android.custom;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class TopBar extends RelativeLayout{
	private Button leftBtn,rightBtn;
	private TextView title;
	private TopBarClickListener topBarClickListener;
	private String titleStr;
	
	private RelativeLayout.LayoutParams leftBtnLayoutParams,rightBtnLayoutParams,titleLayoutParams;
	private static int LEFT_BTN_ID = 1;
	private static int TITLE_ID = 2;
	private static int RIGHT_BTN_ID = 3;
	
	private Drawable leftBackground,rightBackground;
	private String leftText,rightText;
	private int leftTextColor,rightTextColor,titleTextColor;
	private float titleTextSize;
	
	public TopBar(Context context,AttributeSet attrs){
		super(context,attrs);
		
		TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.TopBar);
		
		this.titleStr = ta.getString(R.styleable.TopBar_title);
		this.leftBackground = ta.getDrawable(R.styleable.TopBar_leftBackground);
		this.rightBackground = ta.getDrawable(R.styleable.TopBar_rightBackground);
		this.leftText = ta.getString(R.styleable.TopBar_leftText);
		this.rightText = ta.getString(R.styleable.TopBar_rightText);
		this.leftTextColor = ta.getColor(R.styleable.TopBar_leftTextColor, 0);
		this.rightTextColor = ta.getColor(R.styleable.TopBar_rightTextColor, 0);
		this.titleTextSize = ta.getDimension(R.styleable.TopBar_titleTextSize, 14);
		this.titleTextColor = ta.getColor(R.styleable.TopBar_titleTextColor, 0);
		
		ta.recycle();
		
		leftBtn = new Button(context);
		rightBtn = new Button(context);
		title = new TextView(context);
		
		leftBtn.setId(LEFT_BTN_ID);
		rightBtn.setId(RIGHT_BTN_ID);
		title.setId(TITLE_ID);
		
		
		
		leftBtnLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
		rightBtnLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
		titleLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
		
		leftBtnLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
		leftBtnLayoutParams.setMargins(12, 0, 0, 0);//左上右下
		leftBtnLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
		
		rightBtnLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
		rightBtnLayoutParams.setMargins(12, 0, 0, 0);//左上右下
		rightBtnLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
		
		titleLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
		titleLayoutParams.setMargins(0, 0, 12, 0);//左上右下
		titleLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
		titleLayoutParams.addRule(RelativeLayout.LEFT_OF, RIGHT_BTN_ID);
		titleLayoutParams.addRule(RelativeLayout.RIGHT_OF, LEFT_BTN_ID);
		titleLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
		
		addView(leftBtn, leftBtnLayoutParams);
		addView(rightBtn,rightBtnLayoutParams);
		addView(title,titleLayoutParams);
		
		leftBtn.setBackgroundDrawable(leftBackground);
		leftBtn.setText(leftText);
		leftBtn.setTextColor(leftTextColor);
		rightBtn.setBackgroundDrawable(rightBackground);
		rightBtn.setText(rightText);
		rightBtn.setTextColor(rightTextColor);
		
		title.setTextSize(22.0f);
		title.setTextColor(Color.BLUE);
		title.setEllipsize(TruncateAt.MIDDLE);
		title.setGravity(Gravity.CENTER_HORIZONTAL);
		title.setSingleLine(true);
		title.setText(titleStr);
		title.setTextSize(titleTextSize);
		title.setTextColor(titleTextColor);
		
		leftBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(topBarClickListener!=null){
					topBarClickListener.leftBtnClick();
				}
			}
		});
		
		rightBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(topBarClickListener!=null){
					topBarClickListener.rightBtnClick();
				}
			}
		});
	}

	public void setTopBarClickListener(TopBarClickListener topBarClickListener) {
		this.topBarClickListener = topBarClickListener;
	}
}

package com.android.custom;

public interface TopBarClickListener {

	void leftBtnClick();
	void rightBtnClick();
}

package com.android.custom;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class CustomViewActivity extends Activity{
	private TopBar topBar;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        topBar = (TopBar)findViewById(R.id.topBar);
        topBar.setTopBarClickListener(new TopBarClickListener() {
			@Override
			public void rightBtnClick() {
				//处理右边按钮所对应的逻辑
				Toast.makeText(CustomViewActivity.this, "你点击的是右边的按钮", Toast.LENGTH_LONG).show();
			}
			
			@Override
			public void leftBtnClick() {
				//处理左边按钮所对应的逻辑
				Toast.makeText(CustomViewActivity.this, "你点击的是左边的按钮", Toast.LENGTH_LONG).show();
			}
		});
    }
}

在res/xml文件夹下面新建attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
	<declare-styleable name="TopBar">
		<attr name="title" format="string"/>
		<attr name="titleTextSize" format="dimension"/>
		<attr name="titleTextColor" format="color"/>
		<attr name="leftTextColor" format="color"/>
		<attr name="leftBackground" format="string"/>
		<attr name="leftText" format="string"/>
		<attr name="rightTextColor" format="color"/>
		<attr name="rightBackground" format="string"/>
		<attr name="rightText" format="string"/>
	</declare-styleable>
</resources>


布局文件:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:custom="http://schemas.android.com/apk/res/com.android.custom"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView android:layout_width="fill_parent"
		android:layout_height="fill_parent" android:background="#ff00ff00" />
	<com.android.custom.TopBar android:id="@+id/topBar"
		custom:title="自定义标题" custom:titleTextSize="14.0sp" custom:titleTextColor="#ff0000" android:layout_width="fill_parent"
		custom:leftBackground="@drawable/icon" custom:rightBackground="@drawable/icon"
		custom:leftText="左侧" custom:rightText="右侧"
		custom:leftTextColor="#123123" custom:rightTextColor="#ff0000"
		android:layout_height="wrap_content" android:background="#0000ff">
	</com.android.custom.TopBar>
</FrameLayout>





ps:美化的工作就交给你们自己了,隐藏的属性忘记添加了
  • 大小: 10.9 KB
  • 大小: 11.7 KB
  • 大小: 10.5 KB
  • 大小: 10.1 KB
  • 大小: 18.9 KB
  • 大小: 93.7 KB
   发表时间:2011-10-29  
初学android,在其他地方看到,完全搞不明白,问谷歌,到了这里,看了文章后,明白怎么用了,谢谢了
0 请登录后投票
   发表时间:2011-11-01  
挺好的,很清晰
0 请登录后投票
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics