Android開發之——自定義標題欄titlebar

     本文通過實例介紹一種titlebar的實現方法。

1、activity_main layout 配置文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.my.joe.myneusoft.MainActivity">  ——此處對應的是MainActivity路徑
    <com.my.joe.myneusoft.MyToolBar
        android:id="@+id/myToolBar"
        android:layout_width="fill_parent"
        android:layout_height="50dp"      ——此處爲設置標題欄的屬性
        custom:leftButtonBackground="@null"
        custom:leftButtonText="返回"
        custom:leftButtonTextColor="#0000ff"
        custom:leftButtonTextSize="5sp"      ——此處爲設置左側按鈕的屬性
        custom:rightButtonBackground="@null"
        custom:rightButtonText="菜單"
        custom:rightButtonTextColor="#0000ff"
        custom:rightButtonTextSize="5sp"
—此處爲設置右側按鈕的屬性

custom:textTitle="MyTitle" custom:titleTextColor="#A10000" custom:titleTextSize="5sp" >
—此處爲設置中間標題的屬性



 </com.my.joe.myneusoft.MyToolBar></RelativeLayout>



2、MyToolBar數據處理,包括標題欄的按鈕是否顯示、按鈕的點擊事件

package com.my.joe.myneusoft;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
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;

/*
 * 自定義ToolBar,繼承自RelativeLayout
 **/
public class MyToolBar extends RelativeLayout {

	private Button leftBtn, rightBtn; // 左側和右側的按鈕
	private TextView tvTitle; // 中間的標題

	/*
	 * 中間標題的屬性
	 */
	private String title; // 標題
	private boolean showTitle; // 是否顯示標題
	private float titleSize; // 標題字體大小
	private int titleColor; // 標題字體顏色

	/*
	 * 左邊按鈕的屬性
	 */
	private String leftBtnText;// 左邊按鈕的文字
	private Drawable leftBtnBackground;// 左邊按鈕的背景
	private boolean showLeftBtn;// 是否顯示左邊按鈕
	private int leftBtnTextColor;// 左邊按鈕字體顏色
	private float leftBtnTextSize;// 左邊按鈕字體大小

	/*
	 * 右邊按鈕的屬性
	 */
	private String rightBtnText;// 右邊按鈕的文字
	private Drawable rightBtnBackground;// 右邊按鈕的背景
	private boolean showRightBtn;// 是否顯示右邊按鈕
	private int rightBtnTextColor;// 右邊按鈕字體顏色
	private float rightBtnTextSize;// 右邊按鈕字體大小

	/*
	 * 三個控件的位置配置參數,左側、右側按鈕的位置
	 */
	private LayoutParams titleParams;// 標題的位置配置參數
	private LayoutParams leftParams;// 左邊按鈕的位置配置參數
	private LayoutParams rightParams;// 右邊按鈕的位置配置參數
	// 事件監聽器
	private MyToolBarClickListener listener;

	/*
	 * 在構造函數裏完成自定義toolbar的屬性設置
	 */
	public MyToolBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyToolBar);
		/*
		 * 獲取中間標題的自定義屬性設置以及默認值
		 */
		title = typedArray.getString(R.styleable.MyToolBar_textTitle);
		showTitle = typedArray.getBoolean(R.styleable.MyToolBar_showTitle, true);
		titleSize = typedArray.getDimension(R.styleable.MyToolBar_titleTextSize, 0);
		titleColor = typedArray.getColor(R.styleable.MyToolBar_titleTextColor, 0);
		/*
		 * 獲取左邊按鈕的自定義屬性設置以及默認值
		 */
		leftBtnText = typedArray.getString(R.styleable.MyToolBar_leftButtonText);
		leftBtnTextColor = typedArray.getColor(R.styleable.MyToolBar_leftButtonTextColor, 0);
		leftBtnBackground = typedArray.getDrawable(R.styleable.MyToolBar_leftButtonBackground);
		leftBtnTextSize = typedArray.getDimension(R.styleable.MyToolBar_leftButtonTextSize, 0);
		/*
		 * 獲取右邊按鈕的自定義屬性設置以及默認值
		 */
		rightBtnText = typedArray.getString(R.styleable.MyToolBar_rightButtonText);
		rightBtnTextColor = typedArray.getColor(R.styleable.MyToolBar_rightButtonTextColor, 0);
		rightBtnBackground = typedArray.getDrawable(R.styleable.MyToolBar_rightButtonBackground);
		rightBtnTextSize = typedArray.getDimension(R.styleable.MyToolBar_rightButtonTextSize, 0);
		// 回收資源
		typedArray.recycle();

		/*
		 * 創建三個控件的對象
		 */
		leftBtn = new Button(context);
		rightBtn = new Button(context);
		tvTitle = new TextView(context);
		/*
		 * 設置標題的屬性
		 */
		tvTitle.setText(title);
		tvTitle.setTextColor(titleColor);
		tvTitle.setTextSize(titleSize);
		tvTitle.setGravity(Gravity.CENTER);
		/*
		 * 設置左邊按鈕的屬性
		 */
		leftBtn.setText(leftBtnText);
		leftBtn.setTextColor(leftBtnTextColor);
		//leftBtn.setBackground(leftBtnBackground);
		leftBtn.setTextSize(leftBtnTextSize);
		/*
		 * 設置右邊按鈕的屬性
		 */
		rightBtn.setText(rightBtnText);
		rightBtn.setTextColor(rightBtnTextColor);
		//rightBtn.setBackground(rightBtnBackground);
		rightBtn.setTextSize(rightBtnTextSize);
		// 設置自定義toolbar的背景色
		setBackgroundColor(Color.WHITE);

		// 中間標題的位置參數 配置
		titleParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		titleParams.addRule(RelativeLayout.CENTER_IN_PARENT);
		// 向RelativeLayout中添加控件
		addView(tvTitle, titleParams);

		// 左邊按鈕的位置參數 配置
		leftParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		// 向RelativeLayout中添加控件
		addView(leftBtn, leftParams);

		// 右邊按鈕的位置參數 配置
		rightParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		// 向RelativeLayout中添加控件
		addView(rightBtn, rightParams);

		/*
		 * 左邊按鈕點擊事件
		 */
		leftBtn.setOnClickListener(new OnClickListener() {

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

			}
		});
		/*
		 * 右邊按鈕點擊事件
		 */
		rightBtn.setOnClickListener(new OnClickListener() {

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

			}
		});
	}

	/*
	 * 定義點擊事件的接口
	 */
	public interface MyToolBarClickListener {
		public void leftBtnClick();// 左邊按鈕點擊事件

		public void rightBtnClick();// 右邊按鈕點擊事件
	}

	/*
	 * 自定義的toolbar的事件監聽
	 */
	public void setOnMyToolBarClickListener(MyToolBarClickListener listener) {
		this.listener = listener;
	}

	/*
	 * 設置自定義toolbar的左右兩邊按鈕的是否顯示,默認連個按鈕都是顯示的
	 */
	public void setToolBarBtnVisiable(boolean leftFalg, boolean rightFalg) {
		if (leftFalg && rightFalg) {
			leftBtn.setVisibility(View.VISIBLE);
			rightBtn.setVisibility(View.VISIBLE);
		}
		if (!leftFalg && !rightFalg) {
			leftBtn.setVisibility(View.GONE);
			rightBtn.setVisibility(View.GONE);
		}
		if (!leftFalg && rightFalg) {
			leftBtn.setVisibility(View.GONE);
			rightBtn.setVisibility(View.VISIBLE);
		}
		if (leftFalg && !rightFalg) {
			leftBtn.setVisibility(View.VISIBLE);
			rightBtn.setVisibility(View.GONE);
		}

	}

	/*
	 * 設置自定義的toolbar是否顯示標題,默認是有標題的
	 */
	public void setToolBarTitleVisible(boolean flag) {
		if (flag) {
			tvTitle.setVisibility(View.VISIBLE);
		} else {
			tvTitle.setVisibility(View.GONE);
		}
	}
}

3、MainActivity的具體實現,包括初始化視圖、數據,以及添加事件的監聽器

package com.my.joe.myneusoft;

import com.my.joe.myneusoft.MyToolBar.MyToolBarClickListener;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity {
	private MyToolBar myToolBar;// 自定義toolbar

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 初始化視圖
		initView();
		// 初始化數據
		initData();
		// 事件監聽
		initListener();

	}

	/*
	 * 初始化視圖
	 */
	private void initView() {
		setContentView(R.layout.activity_main);
		myToolBar = (MyToolBar) findViewById(R.id.myToolBar);
	}

	/*
	 * 初始化數據
	 */
	private void initData() {
		// 設置左邊右邊的按鈕是否顯示
		myToolBar.setToolBarBtnVisiable(true, true);
		// 設置是否顯示中間標題,默認的是顯示
		myToolBar.setToolBarTitleVisible(true);
	}

	/*
	 * 事件監聽
	 */
	private void initListener() {
		/*
		 * toolbar的點擊事件處理
		 */
		myToolBar.setOnMyToolBarClickListener(new MyToolBarClickListener() {

			@Override
			public void rightBtnClick() {// 右邊按鈕點擊事件
				Toast.makeText(MainActivity.this, "菜單", Toast.LENGTH_SHORT).show();
			}

			@Override
			public void leftBtnClick() {// 左邊按鈕點擊事件
				Toast.makeText(MainActivity.this, "返回", Toast.LENGTH_SHORT).show();

			}
		});
	}

}

4、在Androidmainfest中需要添加內容,如下

 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@android:style/Theme.Light.NoTitleBar"   ——添加的內容
        >


經過以上的步驟,就可以實現自定義的標題欄,包括左側按鈕、中間標題、右側按鈕,以及按鈕的點擊事件。


發佈了236 篇原創文章 · 獲贊 45 · 訪問量 84萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章