您的位置:首页 > 运维架构

BaseActivity中标题栏TitleBar的封装

2017-06-05 18:37 239 查看
在写程序的时候,很多时候我们都需要用到标题栏,标题栏基本上贯穿于整个APP应用,我们不可能在每个Activity中都单独写一个标题栏的布局,这样代码的复用性和可维护性很差,同时也显得臃肿,因此我们需要把这样共性的东西封装成一个基类TopBaseActivity,在这里面实现功能,让其他Activity继承基类TopBaseActivity就能实现标题栏功能,需要设置标题,切换,点击监听就在子类的Activity中实现。这样岂不是很方便,接下来一步一步的去实现这个功能。效果图如下:



一、创建TopBaseActivity,布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
android:id="@+id/toolsbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#9DA2E2">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="18sp"
android:text="呵呵"/>
</android.support.v7.widget.Toolbar>

<FrameLayout
android:id="@+id/ContentView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

1、Toolbar这个类是设置标题栏的类,中间的TextView表示标题栏名称,这里面可以根据需求进行设置

2、FrameLayout表示填充内容的布局

二、初始化布局

toolbar = (Toolbar) findViewById(R.id.toolsbar);
tvTitle = (TextView) findViewById(R.id.tv_title);
FrameLayout contentView = (FrameLayout) findViewById(R.id.ContentView);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
LayoutInflater.from(this).inflate(getContentView(), contentView);
initTitle();


setSupportActionBar(toolbar);表示使用ToolBar控件替代ActionBar控件。
getSupportActionBar().setDisplayShowTitleEnabled(false);表示不使用图片占位。

getContentView是一个抽象方法,由子类实现,根据子类的布局设置。

initTitle也是一个抽象方法,由子类实现,根据子类的标题设置标题。

protected abstract void initTitle();

protected abstract int getContentView();

public void setTitle(String title) {
tvTitle.setText(title);
}

三、设置点击事件

在标题栏中,一把有个返回按钮和进入按钮,这时候想点击返回或者进入按钮就要设置监听事件了,那么我们在父类的Activity该怎么设置呢?这里通过方法回调进行监听设置。如下:

private OnClickListener clickListenerTopLeft;
private OnClickListener clickListenerTopRight;

public interface OnClickListener {
void onClick();
}

四、设置标题栏两侧数据方法

标题栏两侧数据有可能是文字或者图片,下面通过多个方法,使用方法重载进行数据设置。

protected void setTopLeftButton(int iconResId, OnClickListener onClickListener) {
toolbar.setNavigationIcon(iconResId);
this.clickListenerTopLeft = onClickListener;
}

protected void setTopRightButton(String str, OnClickListener onClickListener) {
this.menuStr = str;
this.clickListenerTopRight = onClickListener;
}
protected void setTopRightButton(int iconResId, OnClickListener onClickListener) {
this.menuIcon = iconResId;
this.clickListenerTopRight = onClickListener;
}
protected void setTopRightButton(String str,int iconResId, OnClickListener onClickListener) {
this.menuStr = str;
this.menuIcon = iconResId;
this.clickListenerTopRight = onClickListener;
}
五、设置标题栏右侧的数据布局

1、重写onCreateOptionsMenu创建菜单Menu

@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!TextUtils.isEmpty(menuStr)||menuIcon!=0) {
getMenuInflater().inflate(R.menu.menu_activity_base_top_bar, menu);
}
return true;
}
2、菜单Menu的布局文件如下

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/menu_1"
android:title=""
app:showAsAction="always"/>
</menu>
六、标题栏左右两侧数据点击事件的初始化

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
if (clickListenerTopLeft != null) {
clickListenerTopLeft.onClick();
}
} else if (item.getItemId() == R.id.menu_1) {
clickListenerTopRight.onClick();
}
return true;
}
七、使用

让Activity继承自TopBaseActivity布局即可,实现抽象方法,设置子类布局

@Override
protected int getContentView() {
return R.layout.activity_main;
}
设置标题,左右侧文字:

setTitle("测试");
setTopLeftButton(R.mipmap.close_seletced, new OnClickListener() {
@Override
public void onClick() {
Toast.makeText(MainActivity.this, "你把我的左边点击了", Toast.LENGTH_SHORT).show();
}
});
setTopRightButton("增加", new OnClickListener() {
@Override
public void onClick() {
Toast.makeText(MainActivity.this, "你把我的右边点文字击了", Toast.LENGTH_SHORT).show();
}
});
怎么样,简单吧,

,源码已上传至http://download.csdn.net/detail/yoonerloop/9861205点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息