Android开发之——自定义标题栏titlebar

来源:互联网 发布:今天的网络怎么了 编辑:程序博客网 时间:2024/05/20 08:43

     本文通过实例介绍一种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() {@Overridepublic void onClick(View v) {listener.leftBtnClick();}});/* * 右边按钮点击事件 */rightBtn.setOnClickListener(new OnClickListener() {@Overridepublic 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@Overrideprotected 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() {@Overridepublic void rightBtnClick() {// 右边按钮点击事件Toast.makeText(MainActivity.this, "菜单", Toast.LENGTH_SHORT).show();}@Overridepublic 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"   ——添加的内容        >


经过以上的步骤,就可以实现自定义的标题栏,包括左侧按钮、中间标题、右侧按钮,以及按钮的点击事件。


0 0
原创粉丝点击