【问题标题】:CollapsingToolBarLayout - status bar scrim color doesn't changeCollapsingToolBarLayout - 状态栏稀松布颜色不会改变
【发布时间】:2016-05-23 02:38:59
【问题描述】:

几天前我更新了我的 android 工作室并开始使用 CoordinatorLayout 和 CollapsingToolbarLayout 只是尝试一些东西。

似乎工具栏稀松布颜色覆盖了状态栏初始颜色和状态栏稀松布颜色(从 xml 和代码都尝试过)

初始状态:


开始滚动:


滚动到折叠:


所以问题是:

  1. 如何防止工具栏在折叠时覆盖状态栏(甚至不让我正在折叠的图像超出它)。

  2. 折叠后如何更改状态栏颜色

我遇到的另一个问题是我给工具栏提供了初始颜色,而不仅仅是稀松布颜色,因为我希望工具栏位于图片上方,而是它会出现在图片的顶部,并且会覆盖其中的一部分以及任何内容那会在它后面崩溃


在 xml 中为工具栏添加了颜色/样式:


  1. 有没有什么办法可以让工具栏从一开始就放在图片上方,然后直接折叠图片? (考虑过可能保持工具栏固定并在图像之前进行框架布局,但它仍然会在状态栏区域崩溃,这是主要问题。)

主要活动xml:

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:contentScrim = "?attr/colorPrimary"
        app:statusBarScrim="?attr/colorAccent" --------> not changing
        android:id="@+id/my_ctl">


        <ImageView
            android:id="@+id/image"
            android:src="@drawable/flights"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:scaleType="centerCrop"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="parallax"
            />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:layout_collapseMode="pin" />


    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>


<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" >

    <include layout="@layout/content_main" />

</android.support.v4.widget.NestedScrollView>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

MainActivity.java:

  CollapsingToolbarLayout ctl = (CollapsingToolbarLayout) findViewById(R.id.my_ctl);
    //ctl.setContentScrimColor(Color.RED);
    ctl.setStatusBarScrimColor(Color.BLUE);  --------> not working
    ctl.setTitle("blabla");

【问题讨论】:

    标签: android android-toolbar android-6.0-marshmallow android-collapsingtoolbarlayout


    【解决方案1】:

    CollapsingToolbarapp:statusBarScrim="?attr/colorAccent"改为app:statusBarScrim="@android:color/transparent"

    然后你可以在你的代码中设置CollapsingToolbar的颜色为ctl.setStatusBarScrimColor(Color.BLUE);

    并确保将android:fitsSystemWindows="true" 添加到AppBarLayout

    【讨论】:

      【解决方案2】:

      尝试将android:fitsSystemWindows="true" 添加到 AppBarLayout。它对我有用。

      【讨论】:

        【解决方案3】:

        对于第 3 点:在工具栏中添加透明背景

        android:background="@android:color/transparent"

         <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:popupTheme="@style/AppTheme.PopupOverlay"
                    app:layout_collapseMode="pin"
                    android:background="@android:color/transparent" />
        

        【讨论】:

          【解决方案4】:

          对于 2) 有a known issue

          现在我正在以编程方式设置工具栏背景的颜色

              mAppBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
                  @Override
                  public void onStateChanged(AppBarLayout appBarLayout, State state) {
                      Toolbar toolbar = (Toolbar) mAppBarLayout.findViewById(R.id.toolbar_top);
                      int toolbarBackground = (state == AppBarStateChangeListener.State.COLLAPSED) ? R.color.color_collapsed : R.color.color_non_collapsed;
                      toolbar.setBackgroundColor(ContextCompat.getColor(DealsOptionsActivity.this, toolbarBackground));
                  }
          
              });
          

          这当然是一个简单的实现。你可以优化它。

          【讨论】:

          • 没有AppBarStateChangeListener,所以可能OP必须这样做:stackoverflow.com/questions/31682310/…
          • 你不应该在使用 offsetlistener 的时候更新你的视图,这非常适合 UI,在非常低端的设备上这会导致 UI 滞后。如果你检查日志,他总是会抛出错误,说明在主线程上画了很多
          • 问题 2 - How can I change status bar colour after collapsing。你的答案是改变Toolbar 背景。
          【解决方案5】:

          一直在寻找解决方案并找到了它。 首先,将您的活动主题设置为 AppThemeNoActionBarLight.SimpleCoordinatorTheme

          看看:

          <!-- Base application theme. -->
          <style name="AppThemeNoActionBarLight" parent="Theme.AppCompat.Light.NoActionBar">
              <!-- Customize your theme here. -->
              <item name="colorPrimary">@color/colorPrimary</item>
              <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
              <item name="colorAccent">@color/colorAccent</item>
          </style>
          
          <style name="AppThemeNoActionBarLight.SimpleCoordinatorTheme">
              <item name="android:windowDrawsSystemBarBackgrounds">true</item>
              <item name="android:statusBarColor">@android:color/transparent</item>
          </style>
          

          然后试试这个布局示例:

          <?xml version="1.0" encoding="utf-8"?>
          <android.support.design.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:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="@android:color/background_light"
          android:fitsSystemWindows="true"
          >
          
          <android.support.design.widget.AppBarLayout
              android:id="@+id/main.appbar"
              android:layout_width="match_parent"
              android:layout_height="300dp"
              android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
              android:fitsSystemWindows="true"
              >
          
              <android.support.design.widget.CollapsingToolbarLayout
                  android:id="@+id/main.collapsing"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:fitsSystemWindows="true"
                  app:layout_scrollFlags="scroll|exitUntilCollapsed"
                  app:contentScrim="?attr/colorPrimary"
                  app:expandedTitleMarginStart="48dp"
                  app:expandedTitleMarginEnd="64dp"
                  app:title="@string/app_name"
                  >
          
                  <ImageView
                      android:id="@+id/main.backdrop"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:scaleType="centerCrop"
                      android:fitsSystemWindows="true"
                      android:src="@drawable/material_flat"
                      app:layout_collapseMode="parallax"
                      tools:ignore="ContentDescription"
                      />
          
                  <android.support.v7.widget.Toolbar
                      android:id="@+id/main.toolbar"
                      android:layout_width="match_parent"
                      android:layout_height="?attr/actionBarSize"
                      app:layout_collapseMode="pin"
                      app:title="Test"
          
                      />
              </android.support.design.widget.CollapsingToolbarLayout>
          </android.support.design.widget.AppBarLayout>
          
          <android.support.v4.widget.NestedScrollView
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              app:layout_behavior="@string/appbar_scrolling_view_behavior"
              >
          
              <TextView
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:textSize="20sp"
                  android:lineSpacingExtra="8dp"
                  android:text="@string/lorem"
                  android:padding="@dimen/activity_horizontal_margin"
                  />
          </android.support.v4.widget.NestedScrollView>
          
          <android.support.design.widget.FloatingActionButton
              android:layout_height="wrap_content"
              android:layout_width="wrap_content"
              android:layout_margin="@dimen/activity_horizontal_margin"
              android:src="@drawable/ic_comment_24dp"
              app:layout_anchor="@id/main.appbar"
              app:layout_anchorGravity="bottom|right|end"
              />
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2014-12-17
            • 1970-01-01
            • 2015-11-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多