Android自定义ToolBar并实现沉浸式的方法

前言

  ToolBar是Android 5.0推出的一个新的导航控件用于取代之前的ActionBar,由于其高度的可定制性、灵活性、具有Material Design风格等优点,越来越多的App也用上了ToolBar。

  沉浸式状态栏是从android Kitkat(Android 4.4)开始出现的,它可以被设置成与APP顶部相同的颜色,这就使得切换APP时,整个界面就好似切换到了与APP相同的风格样式一样。

依赖包:
  Toolbar, implementation 'androidx.appcompat:appcompat:1.1.0'

  沉浸式, implementation 'com.gyf.immersionbar:immersionbar:3.0.0'

1、自定义Toolbar步骤:

1)、定义 /values/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <style name="TextAppearance_TitleBar_Title" parent="TextAppearance.AppCompat.Toolbar.Title">
 ...
 </style>

 <style name="TextAppearance_TitleBar_subTitle" parent="TextAppearance.AppCompat.Toolbar.Subtitle">
  ....
 </style>
</resources>

2)、自定义toolbar 继承 androidx.appcompat.widget.Toolbar

public class CustomToolBar extends Toolbar {
 private TextView mCenterTitle;
 private ImageView mCenterIcon; //中心icon
 private TextView mLeftText;
 private ImageButton mLeftIcon;
 private TextView mSettingText;
 private ImageButton mSettingIcon;

 public CustomToolBar(Context context) {
  super(context);
 }

 public CustomToolBar(Context context, AttributeSet attrs) {
  super(context, attrs);
 }

 public CustomToolBar(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
 }

 public void setLeftText(@StringRes int id) {
  setLeftText(this.getContext().getText(id));
 }

 public CustomToolBar setLeftText(CharSequence text) {
  Context context = this.getContext();
  if (this.mLeftText == null) {
   this.mLeftText = new TextView(context);
   this.mLeftText.setGravity(Gravity.CENTER_VERTICAL);
   this.mLeftText.setSingleLine();
//   this.mLeftText.setEllipsize(TextUtils.TruncateAt.END);
   setLeftTextAppearance(getContext(), R.style.TextAppearance_TitleBar_subTitle);
   //textView in left
//   this.addMyView(this.mLeftText, Gravity.START);
   int i = dp2px(this, 16);
   this.addMyView(this.mLeftText, Gravity.START, 0, 0, i, 0, 48);
  }
  mLeftText.setText(text);
  return this;
 }

 public void setLeftTextAppearance(Context context, @StyleRes int resId) {
  if (this.mLeftText != null) {
   this.mLeftText.setTextAppearance(context, resId);
  }
 }

 public void setLeftTextColor(@ColorInt int color) {
  if (this.mLeftText != null) {
   this.mLeftText.setTextColor(color);
  }
 }

 public void setLeftTextOnClickListener(OnClickListener listener) {
  if (mLeftText != null) {
   mLeftText.setOnClickListener(listener);
  }
 }

 public CustomToolBar setLeftIcon(@DrawableRes int resId) {
  return setLeftIcon(ContextCompat.getDrawable(this.getContext(), resId));
 }

 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
 public CustomToolBar setLeftIcon(Drawable drawable) {
  Context context = this.getContext();
  if (this.mLeftIcon == null) {
   this.mLeftIcon = new ImageButton(context);
    ...

   this.addMyView(this.mLeftIcon, Gravity.START);
  } else {
   if (mLeftIcon.getVisibility() != VISIBLE) {
    mLeftIcon.setVisibility(VISIBLE);
   }
  }
  if (mLeftText != null && mLeftText.getVisibility() != GONE) {
   mLeftText.setVisibility(GONE);
  }
  mLeftIcon.setImageDrawable(drawable);
  return this;
 }

 public void setLeftIconOnClickListener(OnClickListener listener) {
  if (mLeftIcon != null) {
   mLeftIcon.setOnClickListener(listener);
  }
 }

 public void setLeftIconVisibility(int visibility) {
  if (mLeftIcon != null) {
   mLeftIcon.setVisibility(visibility);
  }
 }

 public CustomToolBar setMyCenterTitle(@StringRes int id, boolean center) {
  return setMyCenterTitle(this.getContext().getText(id), center);
 }

 public void setMyCenterTitle(@StringRes int Rid) {
  setMyCenterTitle(this.getContext().getText(Rid));
 }

 public void setMyCenterTitle(CharSequence text) {
  Context context = this.getContext();
  if (this.mCenterTitle == null) {
   this.mCenterTitle = new TextView(context);
   ...

  } else {
   if (this.mCenterTitle.getVisibility() != VISIBLE) {
    mCenterTitle.setVisibility(VISIBLE);
   }
  }
  if (mCenterIcon != null && mCenterIcon.getVisibility() != GONE) {
   mCenterIcon.setVisibility(GONE);
  }

  ...

 }
 public CustomToolBar setMyCenterTitle(CharSequence text, boolean center) {
  Context context = this.getContext();
  if (this.mCenterTitle == null) {
   this.mCenterTitle = new TextView(context);

   ...

  } else {
   if (this.mCenterTitle.getVisibility() != VISIBLE) {
    mCenterTitle.setVisibility(VISIBLE);
   }
  }
  if (mCenterIcon != null && mCenterIcon.getVisibility() != GONE) {
   mCenterIcon.setVisibility(GONE);
  }

  if (!center) {
   setTitle(text);
   setTitleTextColor(getResources().getColor(R.color.black));
  } else {
   mCenterTitle.setText(text);
   mCenterTitle.setTextColor(getResources().getColor(R.color.black));
   mCenterTitle.setTextSize(16);
  }
  return this;
 }

 public void setMyCenterTextAppearance(Context context, @StyleRes int resId) {
  if (this.mCenterTitle != null) {
   this.mCenterTitle.setTextAppearance(context, resId);
  }
 }

 public void setMyCenterTextColor(@ColorInt int color) {
  if (this.mCenterTitle != null) {
   this.mCenterTitle.setTextColor(color);
  }
 }

 public void setMyCenterTextOnClickListener(OnClickListener listener) {
  if (mCenterTitle != null) {
   mCenterTitle.setOnClickListener(listener);
  }
 }

 public void setMyCenterIcon(@DrawableRes int resId) {
  setMyCenterIcon(ContextCompat.getDrawable(this.getContext(), resId));
 }

 public void setMyCenterIcon(Drawable drawable) {
  Context context = this.getContext();
  if (this.mCenterIcon == null) {

   ...

  } else {
   if (mCenterIcon.getVisibility() != VISIBLE) {
    mCenterIcon.setVisibility(VISIBLE);
   }
  }
  if (mCenterTitle != null && mCenterTitle.getVisibility() != GONE) {
   mCenterTitle.setVisibility(GONE);
  }

  setTitle("");
  mCenterIcon.setImageDrawable(drawable);
 }

 public void setMySettingText(@StringRes int Rid) {
  setMySettingText(this.getContext().getText(Rid));
 }

 public void setMySettingText(CharSequence text) {
  Context context = this.getContext();
  if (this.mSettingText == null) {

   ...

  } else {
   if (mSettingText.getVisibility() != VISIBLE) {
    mSettingText.setVisibility(VISIBLE);
   }
  }
  if (mSettingIcon != null && mSettingIcon.getVisibility() != GONE) {
   mSettingIcon.setVisibility(GONE);
  }
  mSettingText.setText(text);
  mSettingText.setTextSize(14);
  mSettingText.setTextColor(getResources().getColor(R.color.toolbar_title));

 }

 public void setMySettingTextAppearance(Context context, @StyleRes int resId) {
  if (mSettingText != null) {
   mSettingText.setTextAppearance(context, resId);
  }
 }

 public void setMySettingTextColor(@ColorInt int color) {
  if (mSettingText != null) {
   mSettingText.setTextColor(color);
  }
 }

 public void setSettingTextOnClickListener(OnClickListener listener) {
  if (mSettingText != null) {
   mSettingText.setOnClickListener(listener);
  }
 }

 public CustomToolBar setMySettingIcon(@DrawableRes int resId) {
  return setMySettingIcon(ContextCompat.getDrawable(this.getContext(), resId));
//  ViewConfiguration.get(this.getContext()).getScaledTouchSlop();
 }

 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
 public CustomToolBar setMySettingIcon(Drawable drawable) {
  Context context = this.getContext();
  if (this.mSettingIcon == null) {

   ...

  } else {
   if (mSettingIcon.getVisibility() != VISIBLE) {
    mSettingIcon.setVisibility(VISIBLE);
   }
  }
  if (mSettingText != null && mSettingText.getVisibility() != GONE) {
   mSettingText.setVisibility(GONE);
  }
  mSettingIcon.setImageDrawable(drawable);
  return this;
 }

 public void setSettingIconOnClickListener(OnClickListener listener) {
  if (mSettingIcon != null) {
   mSettingIcon.setOnClickListener(listener);
  }
 }

 private void addSimpleView(View v, int gravity) {
  addSimpleView(v, gravity, 0, 0, 0, 0);
 }

 private void addSimpleView(View v, int gravity, int left, int top, int right, int bottom) {
  LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
    LayoutParams.WRAP_CONTENT, gravity);
  lp.setMargins(left, top, right, bottom);
  this.addView(v, lp);
 }

 private void addMyView(View v, int gravity) {

  addMyView(v, gravity, 0, 0, dp2px(this, 16), 0);
 }

 private void addMyView(View v, int gravity, int left, int top, int right, int bottom) {
  LayoutParams lp = new LayoutParams(dp2px(this, 20),
    dp2px(this, 20), gravity);
  lp.setMargins(left, top, right, bottom);
  this.addView(v, lp);
 }

 private void addMyView(View v, int gravity, int left, int top, int right, int bottom, int width) {
  LayoutParams lp = new LayoutParams(dp2px(this, width),
    20, gravity);
  lp.setMargins(left, top, right, bottom);
  this.addView(v, lp);
 }

 public CustomToolBar setCenterTitleWithImg(CharSequence text, Drawable drawable, boolean center) {
  Context context = this.getContext();
  if (this.mCenterTitle == null) {
   this.mCenterTitle = new TextView(context);

   ...

   if (this.mCenterTitle.getVisibility() != VISIBLE) {
    mCenterTitle.setVisibility(VISIBLE);
   }
  }

  if (this.mCenterIcon == null) {
   this.mCenterIcon = new ImageView(context);

   ...

  } else {
   if (mCenterIcon.getVisibility() != VISIBLE) {
    mCenterIcon.setVisibility(VISIBLE);
   }
  }

  mCenterTitle.setTextSize(18);
  mCenterTitle.setTextColor(getResources().getColor(R.color.black));
  mCenterTitle.setText(text);
  mCenterIcon.setImageDrawable(drawable);

  return this;
 }

 public void setCenterTitleWithImgOnClickListener(OnClickListener listener) {
  if (mCenterTitle != null) {
   ((View) mCenterTitle.getParent()).setOnClickListener(listener);
  }
 }
}

2、自定义Toolbar使用

1)、res/layout创建布局文件

   <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">

  <com.ktx.view.CustomToolBar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?actionBarSize"
    android:background="@android:color/white"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:titleTextColor="@android:color/black" />

</FrameLayout>

2)、在布局中使用

<?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:binding="http://schemas.android.com/tools">
  <data>
    <variable
      name="viewModel"
      type="com.android.playandroid.viewmodel.LoginViewModel" />
  </data>
  <androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/bkg"
    android:orientation="vertical">

    <include
      layout="@layout/title_layout"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintTop_toTopOf="parent" />
     ...
  </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

3)、代码中使用

a、初始化:

  mBinding.root.toolbar.setNavigationIcon(R.mipmap.register_close)
  mBinding.root.toolbar.setMyCenterTitle(getString(R.string.register), true)
  mBinding.root.toolbar.setMySettingText(getString(R.string.login))

b、点击事件:

  mBinding.root.toolbar.setNavigationOnClickListener {
    ....
  }

  mBinding.root.toolbar.setSettingTextOnClickListener {
    ...
}

4)、沉浸式,设置toolbar背景颜色、文字颜色,一般写在基类

 protected open fun initImmersionBar() {
    //在BaseActivity里初始化
    mImmersionBar = ImmersionBar.with(this)
    if (toolbar != null) {
      mImmersionBar.titleBar(toolbar)
    }
    mImmersionBar.statusBarDarkFont(true)
  //  mImmersionBar.keyboardEnable(true).navigationBarWithKitkatEnable(false).init()
   // mImmersionBar.init()

    ImmersionBar.with(this).init()
  }

Google原生的效果,不必多说,可以实现类似这样的效果

注意:

1、配置整个app的toolbar风格,在/value/styles.xml文件修改代码

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

    <item name="colorPrimary">@android:color/white</item>
    <item name="colorPrimaryDark">@android:color/white</item>
    <item name="colorAccent">@android:color/white</item>

   ...

  </style>

2、修改了 toolbar的高度 ,怎么让navigationIcon显示在toolbar中心?

  只要设置如下,即可:android:minHeight="@dimen/toolbar_height"

3、toolbar布局文件位置

  如果在commonlibrary目录创建该文件,在app 下还需要复制一份,因为在app 使用toolbar,kotlin-android-extensions引用不到commonlibrary目录下的布局文件。

代码Github:https://github.com/AlbertShen0211/PlayAndroid

总结

到此这篇关于Android自定义ToolBar并实现沉浸式的文章就介绍到这了,更多相关Android 自定义ToolBar并沉浸式内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • android顶部(toolbar)搜索框实现代码

    APP中经常会使用到搜索功能,那我们的搜索框该如何布局呢?下面咱们就看一下下面这个效果: xml布局: <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="h

  • Android动态修改ToolBar的Menu菜单示例

    Android动态修改ToolBar的Menu菜单 效果图 实现 实现很简单,就是一个具有3个Action的Menu,在我们滑动到不同状态的时候,把对应的Action隐藏了. 开始上货 Menu Menu下添加3个Item <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xml

  • Android自定义Toolbar使用方法详解

    本篇文章介绍: 如何使用Toolbar; 自定义Toolbar; 先来看一看效果,了解一下toolbar: 布局文件: <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@co

  • Android Toolbar自定义标题标题居中的实例代码

    自定义Toolbar,实现方式如下: 1.布局文件,在activity_main.xml  文件中写入Toolbar <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q

  • Android中Toolbar随着ScrollView滑动透明度渐变效果实现

    Android中Toolbar随着ScrollView滑动透明度渐变效果实现 一.思路:监听ScrollView的滑动事件 不断的修改Toolbar的透明度 二.注意 1.ScrollView 6.0以前没有scrollView.setOnScrollChangeListener(l)方法  所以要自定义ScrollView 在onScrollChanged()中监听 2.ScrollView 6.0(23)以前没有scrollView.setOnScrollChangeListener()方法

  • Android ToolBar控件详解及实例

    ToolBar控件详解 在Activity中添加ToolBar 1.添加库 dependencies { ... compile "com.android.support:appcompat-v7:18.0.+" } 2.Activity要继承AppCompatActivity 3.设置主题 使用ToolBar,要将系统默认的ActionBar隐藏掉 <application android:theme="@style/Theme.AppCompat.Light.NoA

  • Android顶部(toolbar)搜索框实现的实例详解

    Android顶部(toolbar)搜索框实现的实例详解 本文介绍两种SearchView的使用情况,一种是输入框和搜索结果不在一个activity中,另一种是在一个activity中. 首先编写toolbar的布局文件 toolbar中图标在menu文件下定义一个布局文件实现 示例代码: <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.

  • Android中ActionBar和ToolBar添加返回箭头的实例代码

     1.ActionBar添加返回箭头 //onCreate方法中 ActionBar actionBar = this.getSupportActionBar(); actionBar.setTitle("搜索功能"); actionBar.setDisplayHomeAsUpEnabled(true); //activity类中的方法 @Override public boolean onOptionsItemSelected(MenuItem item) { if(item.get

  • Android自定义ActionProvider ToolBar实现Menu小红点

    今天的几个目标: 1. 自定义ActionProvider 2. Toolbar ActionBar自定义Menu 3. Toolbar ActionBar 右侧Menu添加角标(Toolbar ActionBar Menu添加小红点) 源代码在文章末尾. -------------------------------------------------------------------------------- 效果预览 自定义Menu后不影响原生MD的任何效果.可以通过外部来控制显示的文字

  • Android折叠式Toolbar使用完全解析(CollapsingToolbarLayout)

    简介 在各种不同的应用中,大家可能会经常见到这样一个效果:Toolbar是透明的,有着一个背景图片以及大标题,随着页面向上滑动,其标题逐渐缩放到Toolbar上,而背景图片则在滑动到一定程度后变成了Toolbar的颜色,这种效果也即是折叠式效果.其实这种效果在GitHub上面已经有很多开源库实现了,但是Google在其推出的Design Library库中也给出了一个这种控件,让我们很方便地实现了这种效果.这个控件是CollapsingToolbarLayout,它是一个增强型的FrameLay

随机推荐