【问题标题】:Android Toolbar overlapping TabLayout in CollapsingToolbarLayoutAndroid Toolbar 在 CollapsingToolbarLayout 中重叠 TabLayout
【发布时间】:2016-05-15 08:43:55
【问题描述】:

我正在尝试将CollapsingToolbarLayoutToolbarTabLayout 放在它下面,但它们相互重叠,我得到this

我尝试了很多解决方案,但仍然有这个问题。这是我的xml:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:fab="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/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            app:contentScrim="@color/colorAppPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <RelativeLayout
                android:id="@+id/image"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:background="@drawable/material_plane"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                android:src="@drawable/header_png"
                app:layout_collapseMode="parallax"
                app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed">

                <ImageView
                    android:id="@+id/imageViewPhoto"
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:layout_centerInParent="true" />

                <TextView
                    android:id="@+id/textViewName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="false"
                    android:layout_below="@+id/imageViewPhoto"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="5dp"
                    android:text="TEXT"
                    android:textColor="@color/white"
                    android:textSize="16dp" />
            </RelativeLayout>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:fitsSystemWindows="true"
                android:gravity="top"
                app:layout_collapseMode="pin"
                app:layout_scrollFlags="scroll|enterAlways">

                <TextView
                    android:id="@+id/toolbar_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:textColor="@color/white"
                    android:textSize="20dp"
                    android:textStyle="bold" />
            </android.support.v7.widget.Toolbar>

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_gravity="bottom"
                android:fitsSystemWindows="true"
                app:tabBackground="@android:color/transparent"
                app:tabMode="scrollable" />

        </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 android-toolbar overlapping android-coordinatorlayout android-tablayout


    【解决方案1】:

    尝试从您的RelativeLayout 中删除它:

    app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed">
    

    或者就让他们这样吧:

    app:layout_scrollFlags="scroll|enterAlwaysCollapsed">
    

    我猜这是一个错误。


    更新:查看此链接:http://blog.grafixartist.com/parallax-scrolling-tabs-design-support-library/

    还有类似的问题:How to use a TabLayout with Toolbar inside CollapsingToolbarLayout?

    <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            app:contentScrim="@color/colorPrimaryDark"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">
    
            <RelativeLayout
                android:id="@+id/image"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:background="@drawable/header"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop">
    
                <ImageView
                    android:id="@+id/imageViewPhoto"
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:layout_centerInParent="true"
                    app:layout_collapseMode="parallax" />
    
                <TextView
                    android:id="@+id/textViewName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="false"
                    android:layout_below="@+id/imageViewPhoto"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="5dp"
                    android:text="TEXT"
                    android:textSize="16dp" />
    
            </RelativeLayout>
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:fitsSystemWindows="true"
                android:gravity="top"
                app:layout_collapseMode="pin"
                app:layout_scrollFlags="scroll|enterAlways">
    
                <TextView
                    android:id="@+id/toolbar_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:textSize="20dp"
                    android:textStyle="bold" />
            </android.support.v7.widget.Toolbar>
    
            <android.support.design.widget.TabLayout
                android:id="@+id/htab_tabs"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_gravity="bottom"
                app:tabIndicatorColor="@android:color/white" />
    
        </android.support.design.widget.CollapsingToolbarLayout>
    

    【讨论】:

    • 我试过这个,但是我的CollapsingToolbarLayout 变高了,我不能把TabLayout 放进去。 (我需要透明的TabLayout
    • 你想要这样的例子吗? :github.com/TheLittleNaruto/SupportDesignExample我刚刚更新了答案。
    • 所以,答案是正确的,你应该把 TabLayout 放在 CollapsingToolbarLayout 下。如果你想让它透明,你可以检查一下:stackoverflow.com/questions/32372391/… 或 - android:background="@android:color/transparent" 那就是顺便说一句。
    • 好的。我有透明的TabLayout,但我怎样才能把它放得更高(在RelativeLayout 的底部)?我试过margin,但后来又重叠了。
    • 这就是为什么我说你需要把它放在CollapsingToolbarLayout下面检查这个布局:github.com/TheLittleNaruto/SupportDesignExample/blob/master/app/…
    【解决方案2】:

    我也遇到了类似的问题,我尝试从 CoordinatorLayout. 中删除 android:fitsSystemWindows="true" 并且成功了。

    【讨论】:

      【解决方案3】:

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

      android:layout_marginBottom="48dp"
      

      【讨论】:

        【解决方案4】:

        像这样编辑你的 XML:

         <android.support.design.widget.TabLayout
                    android:id="@+id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:layout_gravity="bottom"
                    android:fitsSystemWindows="true"
                    android:marginTop="?attr/actionBarSize"
                    app:tabBackground="@android:color/transparent"
                    app:tabMode="scrollable"/>
        

        如果不起作用请评论

        【讨论】:

          【解决方案5】:

          您必须将您的 TabLayout 保持在 CollapsingToolbarLayout 下方,它会正常工作,您可以从下面看到我的代码:

          <android.support.design.widget.AppBarLayout
              android:id="@+id/appbar"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:fitsSystemWindows="true"
              android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
          
              <android.support.design.widget.CollapsingToolbarLayout
                  android:id="@+id/collapsing_toolbar"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:fitsSystemWindows="true"
                  app:contentScrim="?attr/colorPrimary"
                  app:expandedTitleMarginBottom="@dimen/_32sdp"
                  app:expandedTitleMarginEnd="@dimen/_64sdp"
                  app:expandedTitleMarginStart="@dimen/_48sdp"
                  app:expandedTitleTextAppearance="@style/TextAppearance.AppCompat.Large"
                  app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed">
          
                  <!--  Your View that you want to hide on animation -->
                  <android.support.v4.view.ViewPager
                      android:id="@+id/pager"
                      android:layout_width="match_parent"
                      android:layout_height="@dimen/_250sdp"
                      android:fitsSystemWindows="true"
                      android:scaleType="centerCrop"
                      app:layout_collapseMode="parallax" />
          
                  <!-- Your toolbar should always below your View otherwise it won't be visible  -->
                  <android.support.v7.widget.Toolbar
                      android:id="@+id/toolBar"
                      android:layout_width="match_parent"
                      android:layout_height="?attr/actionBarSize"
                      app:layout_collapseMode="pin"
                      app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
                      app:title="title text"
                      app:titleTextColor="@color/white" />
          
              </android.support.design.widget.CollapsingToolbarLayout>
          
              <!--your tablayout should be here which will come below the toolbar-->
              <android.support.design.widget.TabLayout
                  android:id="@+id/tabs"
                  android:layout_width="match_parent"
                  android:layout_height="@dimen/_50sdp"
                  android:layout_below="@+id/toolbar"
                  android:layout_gravity="bottom"
                  android:background="@color/theme_color"
                  android:overScrollMode="never"
                  android:scrollbars="horizontal"
                  android:visibility="visible"
                  app:layout_anchor="@id/appbar"
                  app:layout_anchorGravity="bottom"
                  app:tabIndicatorColor="@color/orrange"
                  app:tabMode="scrollable" />
          
          </android.support.design.widget.AppBarLayout>
          
          
          <android.support.v4.widget.NestedScrollView
              android:id="@+id/scrollView"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              app:layout_behavior="@string/appbar_scrolling_view_behavior">
          
              <include layout="@layout/content" />
          
          </android.support.v4.widget.NestedScrollView>
          
          </android.support.design.widget.CoordinatorLayout>
          

          一切顺利。

          【讨论】:

            【解决方案6】:

            确保您使用的是以下结构:

            <android.support.design.widget.CoordinatorLayout> 
            <android.support.design.widget.AppBarLayout>
            <android.support.design.widget.CollapsingToolbarLayout>
            <ImageView />
            <android.support.v7.widget.Toolbar />
            <android.support.design.widget.TabLayout />
            </android.support.design.widget.CollapsingToolbarLayout>
            </android.support.design.widget.AppBarLayout>
            </android.support.design.widget.CoordinatorLayout>
            

            你需要确保只有工具栏有:

            android:layout_marginBottom="48dp"
            app:layout_collapseMode="pin"
            

            <android.support.v7.widget.Toolbar
                  android:id="@+id/toolbar"
                  android:layout_width="match_parent"
                  android:layout_height="?attr/actionBarSize"
                  android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                  app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                  android:layout_gravity="top"
                  android:layout_marginBottom="48dp"
                  app:layout_collapseMode="pin"
                  >
            

            CollapsingToolbarLayout必须包含

            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
            

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2015-08-21
              • 2019-04-05
              • 1970-01-01
              • 2015-08-18
              • 1970-01-01
              • 2016-02-18
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多