赞
踩
1.build.gradle下添加(哪个module需databinding就在对应的build.gradle下添加):
- android {
- dataBinding {
- enabled = true
- }
- …………………………
- }
2.标题栏xml布局如下:
- <?xml version="1.0" encoding="utf-8"?>
-
- <layout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto">
-
- <android.support.constraint.ConstraintLayout
- android:id="@+id/title_bar"
- android:layout_width="match_parent"
- android:layout_height="48dp"
- android:background="@color/black">
-
- <TextView
- android:id="@+id/tv_public_back"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentStart="true"
- android:layout_centerVertical="true"
- android:layout_marginLeft="10dp"
- android:drawableLeft="@mipmap/back"
- android:drawablePadding="5dp"
- android:onClick="@{toolbar.clickBack}"
- android:padding="4dp"
- android:text="@{toolbar.back}"
- android:textColor="@color/white"
- android:textSize="16sp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
-
- <TextView
- android:id="@+id/tv_public_title"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:layout_centerHorizontal="true"
- android:layout_centerVertical="true"
- android:onClick="@{toolbar.clickTitle}"
- android:text="@{toolbar.tvTitle}"
- android:textColor="@color/white"
- android:textSize="18sp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
-
- <TextView
- android:id="@+id/tv_public_right"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentEnd="true"
- android:layout_centerVertical="true"
- android:layout_marginRight="10dp"
- android:onClick="@{toolbar.clickRight}"
- android:text="@{toolbar.tvRight}"
- android:textColor="@color/white"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
-
-
- </android.support.constraint.ConstraintLayout>
-
- <data>
-
- <variable
- name="toolbar"
- type="com.example.module_common.common.toolbar.TitleBuilder" />
-
- </data>
- </layout>
3.标题栏的封装类TitleBuilder
- public class TitleBuilder {
- private String back;
- private String tvTitle;
- private String tvRight;
- //三个接口是为了重写点击事件
- private BackClickListener backClickListener;
- private TitleClickListener titleClickListener;
- private RightClickListener rightClickListener;
-
- public static TitleBuilder getActivityToolBar(Activity activity) {
- String back = activity.getString(R.string.public_back);//返回
- TitleBuilder bar = new TitleBuilder();
- bar.backClickListener = new BackClickListener() {
- @Override
- public void clickBack(View view) {
- ActivityMgr.getInstance().finishActivity(activity);//关闭当前activity,使用activity管理类
- }
- };
-
- bar.titleClickListener = new TitleClickListener() {
- @Override
- public void clickTitle(View view) {
-
- }
- };
-
- bar.rightClickListener = new RightClickListener() {
- @Override
- public void clickRight(View view) {
-
- }
- };
- bar.setBack(back);
- return bar;
- }
-
- public void clickBack(View view) {
- if (null != this.backClickListener) {
- this.backClickListener.clickBack(view);
- }
- }
-
- public void clickTitle(View view) {
- if (null != this.titleClickListener) {
- this.titleClickListener.clickTitle(view);
- }
- }
-
- public void clickRight(View view) {
- if (null != this.rightClickListener) {
- this.rightClickListener.clickRight(view);
- }
-
- }
-
-
- public interface BackClickListener {
- void clickBack(View view);
- }
-
- public interface TitleClickListener {
- void clickTitle(View view);
- }
-
- public interface RightClickListener {
- void clickRight(View view);
- }
-
- public String getBack() {
- return back;
- }
-
- public void setBack(String back) {
- this.back = back;
- }
-
- public String getTvTitle() {
- return tvTitle;
- }
-
- public void setTvTitle(String tvTitle) {
- this.tvTitle = tvTitle;
- }
-
- public String getTvRight() {
- return tvRight;
- }
-
- public void setTvRight(String tvRight) {
- this.tvRight = tvRight;
- }
-
- public BackClickListener getBackClickListener() {
- return backClickListener;
- }
-
- public void setBackClickListener(BackClickListener backClickListener) {
- this.backClickListener = backClickListener;
- }
-
- public TitleClickListener getTitleClickListener() {
- return titleClickListener;
- }
-
- public void setTitleClickListener(TitleClickListener titleClickListener) {
- this.titleClickListener = titleClickListener;
- }
-
- public RightClickListener getRightClickListener() {
- return rightClickListener;
- }
-
- public void setRightClickListener(RightClickListener rightClickListener) {
- this.rightClickListener = rightClickListener;
- }
- }
4.BaseActivity
- public class BaseActivity extends AppCompatActivity {
- private TitleBuilder toolbar;
-
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- ActivityMgr.getInstance().addActivity(this);//加入到堆栈,activity管理类
-
- toolbar = TitleBuilder.getActivityToolBar(this);
- }
-
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {//重写返回(只对物理返回键)
- if (keyCode == KEYCODE_BACK) {
- ActivityMgr.getInstance().finishActivity(this);//关闭当前activity,activity管理类
- }
- return super.onKeyDown(keyCode, event);
- }
-
- public TitleBuilder getToolbar() {
- return toolbar;
- }
-
- public void setToolbar(TitleBuilder toolbar) {
- this.toolbar = toolbar;
- }
- }
5.activity_show_tool_bar布局:
注(担心有些朋友没有注意看布局中的<!--***-->提示,这里单独拿出来提示一下):
<include
android:id="@+id/bar"
layout="@layout/public_title"
app:toolbar="@{bar}" />
toolbar是标题栏布局public_title里的<variable name="toolbar" type="**"/>这里要对应上,
bar就是这个页面定义的<variable name="bar" type="**"/>,可以随意改,但是要对应上
- <?xml version="1.0" encoding="utf-8"?>
-
- <layout 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.support.constraint.ConstraintLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".ui.activity.ShowToolBarActivity">
-
- <TextView
- android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="展示公共标题栏ToolBar"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/bar" />
-
- <include
- android:id="@+id/bar"
- layout="@layout/public_title"
- app:toolbar="@{bar}" />
- <!--toolbar是标题栏布局public_title里的<variable name="toolbar" type="**"/>这里要对应上,bar就是这个页面定义的<variable name="bar" type="**"/>,可以随意改,但是要对应上-->
-
- </android.support.constraint.ConstraintLayout>
-
- <data>
-
- <variable
- name="bar"
- type="com.example.module_common.common.toolbar.TitleBuilder" />
-
- </data>
- </layout>
6.ShowToolBarActivity必须继承BaseActivity
- public class ShowToolBarActivity extends BaseActivity {
- private ActivityShowToolBarBinding mBinding;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- mBinding = DataBindingUtil.setContentView(this, R.layout.activity_show_tool_bar);
- mBinding.setBar(getToolbar());
-
- initViwe();
- }
-
- private void initViwe() {
- getToolbar().setBack("不叫返回叫週莫");
- getToolbar().setTvTitle("週莫标题");
- getToolbar().setTvRight("週莫右边");
-
- //重写三个事件
- getToolbar().setBackClickListener(new TitleBuilder.BackClickListener() {
- @Override
- public void clickBack(View view) {
- Toast.makeText(ShowToolBarActivity.this, "我不想关闭,我想留着", Toast.LENGTH_SHORT).show();
- }
- });
-
- getToolbar().setTitleClickListener(new TitleBuilder.TitleClickListener() {
- @Override
- public void clickTitle(View view) {
- Toast.makeText(ShowToolBarActivity.this, "你点击了週莫标题", Toast.LENGTH_SHORT).show();
- }
- });
-
- getToolbar().setRightClickListener(new TitleBuilder.RightClickListener() {
- @Override
- public void clickRight(View view) {
- Toast.makeText(ShowToolBarActivity.this, "你点击了週莫右边", Toast.LENGTH_SHORT).show();
- }
- });
- }
- }
7.一气呵成,收工。有不足之处,请评论区指出,谢谢!
效果图如下:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。