【问题标题】:Material Design - Toolbar and Tab Layout overlaps when scrollingMaterial Design - 滚动时工具栏和选项卡布局重叠
【发布时间】:2017-09-08 12:39:33
【问题描述】:

我正在设计一个带有标签布局的折叠工具栏。当折叠工具栏布局完全显示时,它会显示为我想要的。

即使发生任何滚动,我也想固定工具栏和标签栏。但是当appbar折叠时,toolbar和tablayout重叠如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    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="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:titleEnabled="false"
            android:fitsSystemWindows="true">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/image"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                android:fitsSystemWindows="true"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:gravity="top"
                android:minHeight="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:titleMarginTop="13dp"
                app:title="sample_pager_tabs_parallax_image"/>

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                app:layout_collapseMode="pin"/>


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


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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

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

我把

android:layout_marginTop="100dp"
android:fitsSystemWindows="true"

在 TabLayout 内,但它不起作用。

将 TabLayout 放在 CollapsingToolbarLayout 之外解决了重叠问题。但是,我想使视差图像显示在状态栏、工具栏和选项卡栏中,如下所示:

【问题讨论】:

    标签: android material-design


    【解决方案1】:

    CollapsingToolbarLayout 中取出TabLayout 并将其移至AppBarLayout,这应该可以解决您的问题。

    <?xml version="1.0" encoding="utf-8"?>
        <android.support.design.widget.CoordinatorLayout
            android:id="@+id/main_content"
            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="match_parent">
    
            <android.support.design.widget.AppBarLayout
                android:id="@+id/appbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                android:fitsSystemWindows="true">
    
                <android.support.design.widget.CollapsingToolbarLayout
                    android:id="@+id/collapsing_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:contentScrim="?attr/colorPrimary"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed"
                    app:titleEnabled="false"
                    android:fitsSystemWindows="true">
    
                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/image"
                        android:scaleType="centerCrop"
                        app:layout_collapseMode="parallax"
                        android:fitsSystemWindows="true"/>
    
                    <android.support.v7.widget.Toolbar
                        android:id="@+id/toolbar"
                        android:layout_width="match_parent"
                        android:layout_height="?attr/actionBarSize"
                        android:gravity="top"
                        android:minHeight="?attr/actionBarSize"
                        app:layout_collapseMode="pin"
                        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                        app:titleMarginTop="13dp"
                        app:title="sample_pager_tabs_parallax_image"/>
                </android.support.design.widget.CollapsingToolbarLayout>
    
                <android.support.design.widget.TabLayout
                    android:id="@+id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    app:layout_collapseMode="pin"/>
    
            </android.support.design.widget.AppBarLayout>
    
            <android.support.v4.view.ViewPager
                android:id="@+id/viewpager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    
        </android.support.design.widget.CoordinatorLayout>
    

    【讨论】:

    • 将 TabLayout 放在 CollapsingToolbarLayout 之外解决了重叠问题。但是,我想在状态栏、工具栏和标签栏显示视差图像。
    • 这不是您最初问题的上下文,您能否对其进行编辑以详细说明所需的结果?确实这应该是一个新问题,因为您之前没有提到它。
    【解决方案2】:

    我找到了解决这个问题的方法,只需在你的工具栏布局中添加这一行。

    android:layout_marginBottom="48dp"
    

    【讨论】:

      【解决方案3】:

      做这些事情:

      1.将&lt;android.support.design.widget.TabLayoutCollapsingToolbarLayout中取出并添加到AppBarLayout

      2.从CoordinatorLayout中删除android:fitsSystemWindows="true"

      3.同时将app:layout_scrollFlags="scroll|enterAlways" 应用到Toolbar 以使其固定在顶部

      【讨论】:

      • 将 TabLayout 移出 CollapsingToolbarLayout 解决了重叠问题。但是,我想在状态栏、工具栏和标签栏显示视差图像。
      • 我建议将您的 imageview 表单折叠工具栏布局放到 ....appbarlayout 并使 tablayout 背景透明
      【解决方案4】:
      1. 更改您的Toolbar 高度。使用android:layout_height="104dp" 而不是android:layout_height="?attr/actionBarSize"
      2. TabLayout 中删除属性app:layout_collapseMode="pin"

      试试这个:

      <?xml version="1.0" encoding="utf-8"?>
      <android.support.design.widget.CoordinatorLayout
          android:id="@+id/main_content"
          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="match_parent"
          android:fitsSystemWindows="true">
      
          <android.support.design.widget.AppBarLayout
              android:id="@+id/appbar"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
              android:fitsSystemWindows="true">
      
              <android.support.design.widget.CollapsingToolbarLayout
                  android:id="@+id/collapsing_toolbar"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  app:contentScrim="?attr/colorPrimary"
                  app:layout_scrollFlags="scroll|exitUntilCollapsed"
                  app:titleEnabled="false"
                  android:fitsSystemWindows="true">
      
                  <ImageView
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:background="@drawable/image"
                      android:scaleType="centerCrop"
                      app:layout_collapseMode="parallax"
                      android:fitsSystemWindows="true"/>
      
                  <android.support.v7.widget.Toolbar
                      android:id="@+id/toolbar"
                      android:layout_width="match_parent"
                      android:layout_height="104dp"
                      android:gravity="top"
                      android:minHeight="?attr/actionBarSize"
                      app:layout_collapseMode="pin"
                      app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                      app:titleMarginTop="13dp"
                      app:title="sample_pager_tabs_parallax_image"/>
      
                  <android.support.design.widget.TabLayout
                      android:id="@+id/tabs"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_gravity="bottom"/>
      
              </android.support.design.widget.CollapsingToolbarLayout>    
          </android.support.design.widget.AppBarLayout>
      
          <android.support.v4.view.ViewPager
              android:id="@+id/viewpager"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
      
      </android.support.design.widget.CoordinatorLayout>
      

      【讨论】:

        猜你喜欢
        • 2017-08-22
        • 2016-02-01
        • 2015-12-28
        • 1970-01-01
        • 2015-09-03
        • 2015-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多