ToolBar 封装及在 BaseActivity 和 Base
2019-02-21 本文已影响3人
wuchao226
ToolBar 常用属性和menu的使用及溢出菜单改变 请参考 ToolBar详解。
一、ToolBar 布局自定义
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/toolBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:theme="@style/AppTheme"
app:navigationIcon="@drawable/icon_back">
<android.support.v7.widget.AppCompatTextView
android:id="@+id/tv_title"
style="@style/WrapWrap"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:gravity="center"
android:singleLine="true"
android:text=""
android:textColor="@color/common_white"
android:textSize="@dimen/text_large_size"/>
<android.support.v7.widget.AppCompatTextView
android:id="@+id/tv_right"
style="@style/WrapWrap"
android:layout_height="match_parent"
android:layout_gravity="right|center_vertical"
android:drawablePadding="5dp"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:gravity="center"
android:paddingLeft="@dimen/common_padding"
android:paddingRight="@dimen/common_padding"
android:textColor="@color/common_white"
tools:ignore="RtlHardcoded,RtlSymmetry"/>
</android.support.v7.widget.Toolbar>
二、ToolBar 帮助类 ToolBarHelper.class 封装
package com.sliver.base.widgets;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.ContentFrameLayout;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import com.sliver.base.R;
/**
* @desciption: toolBar 帮助类
*/
public class ToolBarHelper {
/**
* 两个属性
* 1,toolbar是否悬浮在窗口之上
* 2,toolbar的高度获取
*/
private static int[] ATTRS = {
R.attr.windowActionBarOverlay,
R.attr.actionBarSize
};
/**
* 创建view时用到
*/
private Context mContext;
/**
* base setView
*/
private ContentFrameLayout mContentView;
/**
* 用户定义的视图
*/
private View mUserView;
private Toolbar mToolbar;
/**
* 视图构造器
*/
private LayoutInflater mInflater;
private AppCompatTextView mTvTitle;
private AppCompatTextView mTvRight;
public ToolBarHelper(Context context, Object view) {
this.mContext = context;
mInflater = LayoutInflater.from(mContext);
if (view instanceof Integer) {
mUserView = mInflater.inflate((Integer) view, null);
} else if (view instanceof View) {
mUserView = (View) view;
} else {
throw new ClassCastException("ToolBarHelper setLayout() type must be int or View");
}
//初始化整个内容
initContentView();
//初始化用户定义的布局
intiUserView();
//初始化toolBar
initToolBar();
}
@SuppressLint("RestrictedApi")
private void initContentView() {
//直接创建一个帧布局,作为视图容器的父容器
mContentView = new ContentFrameLayout(mContext);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mContentView.setLayoutParams(params);
}
private void intiUserView() {
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//设置自定义属性到控件中
TypedArray typedArray = mContext.getTheme().obtainStyledAttributes(ATTRS);
//主题中定义的悬浮标志
boolean overly = typedArray.getBoolean(0, false);
//获取主题中定义的toolBar的高度
@SuppressLint({"ResourceType", "PrivateResource"})
int toolBarSize = (int) typedArray.getDimension(1,
mContext.getResources().getDimension(
android.support.design.R.dimen.abc_action_bar_default_height_material));
typedArray.recycle();
//如果是悬浮状态,则不需要设置间距
params.topMargin = overly ? 0 : toolBarSize;
mContentView.addView(mUserView, params);
}
private void initToolBar() {
/*
* 通过inflater获取toolBar的布局文件
*/
View toolBar = mInflater.inflate(R.layout.toolbar_layout, mContentView);
mToolbar = toolBar.findViewById(R.id.toolBar);
mTvTitle = toolBar.findViewById(R.id.tv_title);
mTvRight = toolBar.findViewById(R.id.tv_right);
}
public ContentFrameLayout getContentView() {
return mContentView;
}
public Toolbar getToolBar() {
return mToolbar;
}
/**
* 获取中间视图
*
* @return 中间视图
*/
public AppCompatTextView getTitleView() {
return mTvTitle;
}
/**
* 设置中间标题
*
* @param txt 文本
*/
public void setTitleText(String txt) {
mTvTitle.setText(txt);
}
/**
* 获取右侧视图
*
* @return 右侧视图
*/
public AppCompatTextView getRightView() {
return mTvRight;
}
/**
* 设置右侧标题
*
* @param txt 文本
*/
public void setRightText(String txt) {
mTvRight.setText(txt);
}
}
三、ToolBar 在 Activity 中的封装使用
//Activity基类,业务无关
public abstract class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(setLayoutId());
onBindView(savedInstanceState);
}
/**
* 布局
*
* @return 布局资源
*/
protected abstract int setLayoutId();
/**
* 初始化view
*
* @param savedInstanceState 参数
*/
protected abstract void onBindView(Bundle savedInstanceState);
}
//Activity基类,业务无关 带有Toolbar
public abstract class BaseToolbarActivity extends BaseActivity {
public Toolbar mToolbar;
private ToolBarHelper mToolBarHelper;
@Override
public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
initToolBar(layoutResID);
}
private void initToolBar(int layoutResID) {
mToolBarHelper = new ToolBarHelper(this, layoutResID);
mToolbar = mToolBarHelper.getToolBar();
//把toolBar设置到Activity中
setContentView(mToolBarHelper.getContentView());
setSupportActionBar(mToolbar);
onCreateCustomerToolbar(mToolbar);
/*
* 右侧视图监听
*/
mToolBarHelper.getRightView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onClickRight();
}
});
}
public void onCreateCustomerToolbar(Toolbar toolbar) {
//插入toolbar视图的内容的起始点与结束点
toolbar.setContentInsetsRelative(0, 0);
//隐藏标题和子标题
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
}
/**
* 右边的标题的监听事件
*/
public void onClickRight() {
}
/**
* 隐藏返回按钮
*/
public void hideBack() {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* 设置标题
*
* @param title 标题
*/
public void setTitleText(String title) {
mToolBarHelper.setTitleText(title);
}
/**
* 设置右边的标题文字
*
* @param title 标题
*/
public void setRightTitle(String title) {
mToolBarHelper.setRightText(title);
}
/**
* 获取右边的view
*/
public TextView getRightView() {
return mToolBarHelper.getRightView();
}
/**
* 获取中间视图
*/
public TextView getTitleView() {
return mToolBarHelper.getTitleView();
}
}
以上就是 ToolBar 在 Activity 中封装的关键代码,在使用时只要继承基类 BaseToolbarActivity 即可。
四、ToolBar 在 Fragment 中的封装使用
// Fragment 基类
public abstract class BaseFragment extends Fragment {
protected BaseActivity mActivity;
@Override
public void onAttach(Context context) {
super.onAttach(context);
mActivity = (BaseActivity) context;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mActivity = (BaseActivity) activity;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView;
if (setLayout() instanceof Integer) {
rootView = inflater.inflate((Integer) setLayout(), container, false);
} else if (setLayout() instanceof View) {
rootView = (View) setLayout();
} else {
throw new ClassCastException("BaseFragment setLayout() type must be int or setView");
}
onBindView(savedInstanceState, rootView);
return rootView;
}
/**
* 布局
*
* @return object
*/
protected abstract Object setLayout();
/**
* setView 初始化
*
* @param savedInstanceState savedInstanceState
* @param rootView rootView
*/
protected abstract void onBindView(@Nullable Bundle savedInstanceState, View rootView);
}
//Fragment实现的基类 带有Toolbar
public abstract class BaseToolBarFragment extends BaseFragment{
public Toolbar mToolbar;
private ToolBarHelper mToolBarHelper;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//指出fragment愿意添加item到选项菜单
setHasOptionsMenu(true);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mToolBarHelper = new ToolBarHelper(mActivity, setLayout());
mToolbar = mToolBarHelper.getToolBar();
mActivity.setSupportActionBar(mToolbar);
onCreateCustomToolBar(mToolbar);
setToolbarListener();
ContentFrameLayout contentView = mToolBarHelper.getContentView();
onBindView(savedInstanceState, contentView);
return contentView;
}
}
protected void onCreateCustomToolBar(Toolbar toolbar) {
//插入toolbar视图的内容的起始点与结束点
toolbar.setContentInsetsRelative(0, 0);
//隐藏标题和子标题
if (mActivity.getSupportActionBar() != null) {
mActivity.getSupportActionBar().setDisplayShowTitleEnabled(false);
}
}
private void setToolbarListener() {
mToolBarHelper.getRightView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onClickRight();
}
});
}
/**
* 右边的标题的监听事件
*/
public void onClickRight() {
}
/**
* 隐藏返回按钮
*/
public void hideBack() {
mActivity.getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
/**
* 设置标题
*
* @param title 标题
*/
public void setTitleText(String title) {
mToolBarHelper.setTitleText(title);
}
/**
* 设置右边的标题文字
*
* @param title 标题
*/
public void setRightTitle(String title) {
mToolBarHelper.setRightText(title);
}
/**
* 获取右边的view
*/
public TextView getRightView() {
return mToolBarHelper.getRightView();
}
/**
* 获取中间视图
*/
public TextView getTitleView() {
return mToolBarHelper.getTitleView();
}
以上就是 ToolBar 在 Fragment 中封装的关键代码,在使用时只要继承基类 BaseToolbarFragment 即可。