自定义TopBar

1、分析自定义的TopBar所需要的属性,包括左按钮的字体颜色,字体内容,背景(leftTextColor,leftText,leftBackground),右按钮的字体颜色,字体内容,背景(rightTextColor,rightText,rigthtBackground),及标题的字体内容,字体颜色,字体大小(title,titleTextColor,titleTextSize)。在res目录下新建attrs文件,代码如下

<?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="reference|color"/>
        <attr name="leftText" format="string"/>
        <attr name="rightTextColor" format="color"/>
        <attr name="rightBackground" format="reference|color"/>
        <attr name="rightText" format="string"/>
    </declare-styleable>
</resources>

2、实现一个我们自己的TopBar。可加入左右按钮点击事件,采用接口回调的方式,达到代码复用的目的。

package com.example.activity;

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

public class TopBar extends RelativeLayout {

	private Button leftButton, rightButton;
	private TextView tvTitle;

	private int leftTextColor;
	private Drawable leftBackground;
	private String leftText;

	private int rightTextColor;
	private Drawable rightBackground;
	private String rightText;

	private float titleTextSize;
	private int titleTextColor;
	private String title;

	private LayoutParams leftParams, rightParams, titleParams;
	
	private TopbarClickListener listener;
	public interface TopbarClickListener{
		public void leftClick();
		public void rightClick();
	}
	
	public void setOnTopClickListener(TopbarClickListener listener){
		this.listener = listener;
	}

	public TopBar(final Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray tArray = context.obtainStyledAttributes(attrs,
				R.styleable.Topbar);
		leftTextColor = tArray.getColor(R.styleable.Topbar_leftTextColor, 0);
		leftBackground = tArray.getDrawable(R.styleable.Topbar_leftBackground);
		leftText = tArray.getString(R.styleable.Topbar_leftText);

		rightTextColor = tArray.getColor(R.styleable.Topbar_rightTextColor, 0);
		rightBackground = tArray
				.getDrawable(R.styleable.Topbar_rightBackground);
		rightText = tArray.getString(R.styleable.Topbar_rightText);

		titleTextSize = tArray
				.getDimension(R.styleable.Topbar_titleTextSize, 0);
		titleTextColor = tArray.getColor(R.styleable.Topbar_titleTextColor, 0);
		title = tArray.getString(R.styleable.Topbar_title);

		tArray.recycle();

		leftButton = new Button(context);
		rightButton = new Button(context);
		tvTitle = new TextView(context);

		leftButton.setTextColor(leftTextColor);
		leftButton.setBackground(leftBackground);
		leftButton.setText(leftText);
		rightButton.setTextColor(rightTextColor);
		rightButton.setBackground(rightBackground);
		rightButton.setText(rightText);
		tvTitle.setText(title);
		tvTitle.setTextSize(titleTextSize);
		tvTitle.setTextColor(titleTextColor);
		tvTitle.setGravity(Gravity.CENTER);

		setBackgroundColor(0xcccccccc);

		leftParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
		addView(leftButton, leftParams);

		rightParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
		addView(rightButton, rightParams);

		titleParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.MATCH_PARENT);
		titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
		addView(tvTitle, titleParams);

		leftButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				listener.leftClick();
			}
		});

		rightButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				listener.rightClick();
			}
		});

	}

}
3、使用我们的topBar。

xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res/com.example.activity"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${packageName}.${activityClass}" >

    <com.example.activity.TopBar
        android:id="@+id/topBar"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        custom:leftTextColor="#000000"
        custom:leftBackground="#123456"
        custom:leftText="Back"
        custom:rightBackground="#123456"
        custom:rightText="More"
        custom:rightTextColor="#000000"
        custom:title="标题"
        custom:titleTextColor="#123412" 
        custom:titleTextSize="15sp"/>

</RelativeLayout


在Activity中引用。

TopBar topBar = (TopBar) findViewById(R.id.topBar);
		topBar.setOnTopClickListener(new TopbarClickListener() {

			@Override
			public void rightClick() {
				// TODO Auto-generated method stub
				Toast.makeText(TopBarActivity.this, "you clicked the rightBtn", 0).show();
			}

			@Override
			public void leftClick() {
				// TODO Auto-generated method stub
				Toast.makeText(TopBarActivity.this, "you clicked the leftBtn", 0).show();
			}
		});




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值