toolbar coordinatorlayout

TopAppBarMainDemoFragment

<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

  <com.google.android.material.appbar.AppBarLayout
      android:id="@+id/appbarlayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:fitsSystemWindows="true"
      app:liftOnScroll="true">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        style="?attr/catalogToolbarWithCloseButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:title="@string/cat_topappbar_main_demo_toolbar_title"/>
  </com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

Compress Effect Demo

<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
  android:layout_height="match_parent"
    android:fitsSystemWindows="true">

  <com.google.android.material.appbar.AppBarLayout
      android:id="@+id/appbarlayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:fitsSystemWindows="true">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        style="?attr/catalogToolbarWithCloseButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_scrollFlags="scroll|enterAlways|snap"
        app:layout_scrollEffect="compress"
        app:title="@string/cat_topappbar_compress_toolbar_title" />

Scrolling Demo

<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

  <com.google.android.material.appbar.AppBarLayout
      android:id="@+id/appbarlayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:fitsSystemWindows="true">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        style="?attr/catalogToolbarWithCloseButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_scrollFlags="scroll|enterAlways|snap"
        app:title="@string/cat_topappbar_scrolling_demo_toolbar_title" />
  </com.google.android.material.appbar.AppBarLayout>
TopAppBarScrollingTransparentStatusDemoActivity (Scrolling Demo transparent)
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

  <com.google.android.material.appbar.AppBarLayout
      android:id="@+id/appbarlayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:fitsSystemWindows="true"
      app:statusBarForeground="?attr/colorPrimarySurface">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        style="?attr/catalogToolbarWithCloseButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_scrollFlags="scroll|enterAlways|snap" />
  </com.google.android.material.appbar.AppBarLayout>

Preference Demo  TopAppbarPreferencesFragement

<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

  <com.google.android.material.appbar.AppBarLayout
      android:id="@+id/appbarlayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:fitsSystemWindows="true"
      app:liftOnScrollTargetViewId="@id/recycler_view">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        style="?attr/catalogToolbarWithCloseButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:title="@string/cat_topappbar_preferences_toolbar_title" />

  </com.google.android.material.appbar.AppBarLayout>

Collapsing Demo(Medium ) TopAppBarCollapsingMediumDemo 

<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <com.google.android.material.appbar.AppBarLayout
      android:id="@+id/appbarlayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:fitsSystemWindows="true">

    <com.google.android.material.appbar.CollapsingToolbarLayout
        style="?attr/collapsingToolbarLayoutMediumStyle"
        android:id="@+id/collapsingtoolbarlayout"
        android:layout_width="match_parent"
        android:layout_height="?attr/collapsingToolbarLayoutMediumSize"
        app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

      <androidx.appcompat.widget.Toolbar
          android:id="@+id/toolbar"
          style="?attr/catalogToolbarWithCloseButtonStyle"
          android:layout_width="match_parent"
          android:layout_height="?attr/actionBarSize"
          android:elevation="0dp"
          app:layout_collapseMode="pin"
          app:title="@string/cat_topappbar_collapsing_demo_toolbar_medium_title"/>
    </com.google.android.material.appbar.CollapsingToolbarLayout>
  </com.google.android.material.appbar.AppBarLayout>

Collapsing Demo(Large )  TopAppBarCollapsingLargeDemo 

<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <com.google.android.material.appbar.AppBarLayout
      android:id="@+id/appbarlayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:fitsSystemWindows="true">

    <com.google.android.material.appbar.CollapsingToolbarLayout
        style="?attr/collapsingToolbarLayoutLargeStyle"
        android:id="@+id/collapsingtoolbarlayout"
        android:layout_width="match_parent"
        android:layout_height="?attr/collapsingToolbarLayoutLargeSize"
        app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

      <androidx.appcompat.widget.Toolbar
          android:id="@+id/toolbar"
          style="?attr/catalogToolbarWithCloseButtonStyle"
          android:layout_width="match_parent"
          android:layout_height="?attr/actionBarSize"
          android:elevation="0dp"
          app:layout_collapseMode="pin"
          app:title="@string/cat_topappbar_collapsing_demo_toolbar_large_title"/>
    </com.google.android.material.appbar.CollapsingToolbarLayout>
  </com.google.android.material.appbar.AppBarLayout>

Collapsing Demo(MultiLine ) TopAppBarCollapsingMultilineDemo 

<androidx.coordinatorlayout.widget.CoordinatorLayout
  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/coordinator"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:fitsSystemWindows="true">

  <com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appbarlayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true">

    <com.google.android.material.appbar.CollapsingToolbarLayout
      style="?attr/collapsingToolbarLayoutLargeStyle"
      android:id="@+id/collapsingtoolbarlayout"
      android:layout_width="match_parent"
      android:layout_height="?attr/collapsingToolbarLayoutLargeSize"
      app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
      app:maxLines="3">

      <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        style="?attr/catalogToolbarWithCloseButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:elevation="0dp"
        app:layout_collapseMode="pin"
        app:title="@string/cat_topappbar_collapsing_multiline_demo_toolbar_title"
        tools:ignore="UnusedAttribute" />
    </com.google.android.material.appbar.CollapsingToolbarLayout>
  </com.google.android.material.appbar.AppBarLayout>

补充一点浸入式状态栏的设计 配置颜色 或者设置图片都可以

<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

  <com.google.android.material.appbar.AppBarLayout
      android:id="@+id/appbarlayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:fitsSystemWindows="true"
    android:background="@color/green_reference"
      app:liftOnScroll="true">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"

        style="?attr/catalogToolbarWithCloseButtonStyle"
        android:layout_width="match_parent"
      android:layout_height="wrap_content"
        app:title="@string/cat_topappbar_main_demo_toolbar_title"/>
  </com.google.android.material.appbar.AppBarLayout>

滑动的时候会改变颜色 颜色跟随app theme

 

三、主流热门的滑动固定改变状态栏透明的实现

  NestedScrollView scrollView = view.findViewById(R.id.scrollview);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
      scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
        @Override
        public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

          Log.i("DHG", "scollY is :" + scrollY + "toolbar height:" + toolbar.getHeight());
          // 设置 Toolbar 的透明度
          int toolbarHeight = toolbar.getHeight();
          if (scrollY > toolbarHeight) {
            toolbar.getBackground().setAlpha(255);
            toolbar.setBackgroundColor(getResources().getColor(R.color.red_500));

          } else {
            float alpha = (float) scrollY / toolbarHeight * 255;
            toolbar.setBackgroundColor(getResources().getColor(R.color.red_500));
            toolbar.getBackground().setAlpha((int) alpha);
          }
        }
      });
    }
    return view;
  }

 然后xml里用的最标准的不会toolbar固定的样式 注意将toolbar的

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/coordinator"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@drawable/dog_image"
  android:fitsSystemWindows="true">

  <com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appbarlayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/mtrl_btn_transparent_bg_color"
    android:fitsSystemWindows="true"
    app:liftOnScroll="true">

    <androidx.appcompat.widget.Toolbar
      android:id="@+id/toolbar"
      style="?attr/catalogToolbarWithCloseButtonStyle"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@color/mtrl_btn_transparent_bg_color"
    android:fitsSystemWindows="true"//加上 这行very import 不然 状态栏不会跟着滑动的toolbar动态变色 其实就相当于顶上去了
      app:title="@string/cat_topappbar_main_demo_toolbar_title" />
  </com.google.android.material.appbar.AppBarLayout>

  <androidx.core.widget.NestedScrollView
    android:id="@+id/scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值