【问题标题】:Why Toolbar is overlaid with TabLayout?为什么 Toolbar 会被 TabLayout 覆盖?
【发布时间】:2017-05-08 22:38:52
【问题描述】:

我不知道为什么当标题向上滚动时工具栏会被 Tablayout 覆盖。

但我只是希望它们不要像这张图片那样相互重叠

当标题向上滚动并折叠时,我希望看起来像最后一张图片。 我该如何解决? 请帮我。 提前谢谢你。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

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

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            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/collapsingToolbarLayout"
                android:layout_width="match_parent"
                android:layout_height="@dimen/app_bar_height"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <ImageView
                    android:id="@+id/headerBg"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerCrop"
                    android:contentDescription="@string/app_name"
                    app:layout_collapseMode="parallax"
                    app:layout_collapseParallaxMultiplier="0.7" />

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <Space
                        android:layout_width="40dp"
                        android:layout_height="40dp" />

                    <ImageView
                        android:id="@+id/profilePicture"
                        android:layout_width="@dimen/profile_pic_width"
                        android:layout_height="@dimen/profile_pic_height"
                        android:scaleType="centerCrop"
                        android:layout_gravity="center_horizontal"/>
                    <TextView
                        android:id="@+id/userName"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:textStyle="bold"
                        android:textSize="14dp" />
                </LinearLayout>

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

                <android.support.design.widget.TabLayout
                    android:id="@+id/tabLayout"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:layout_gravity="bottom"
                    app:layout_collapseMode="pin"
                    app:tabIndicatorColor="?attr/colorAccent"
                    app:tabIndicatorHeight="3dp"/>

            </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.support.design.widget.NavigationView
        android:id="@+id/navigation"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#fff"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/navigation_drawer_items"
        app:itemIconTint="?attr/colorPrimary"
        app:itemTextColor="@color/black">

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

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

【问题讨论】:

标签: android android-layout android-toolbar android-tablayout android-collapsingtoolbarlayout


【解决方案1】:

我完全不确定,但可能是因为您在整个布局中使用了滚动。只需添加一些 android:layout_marginTop,例如 60 dp。

【讨论】:

    【解决方案2】:

    尝试将fitsSystemWindows=true 放在标签布局中,如下所示:

        <android.support.design.widget.TabLayout
                            android:id="@+id/tabLayout"
                            android:layout_width="match_parent"
                            android:layout_height="?attr/actionBarSize"
                            android:layout_gravity="bottom"
                            app:layout_collapseMode="pin"
                            android:fitsSystemWindows="true"
                            app:tabIndicatorColor="?attr/colorAccent"
                            app:tabIndicatorHeight="3dp"/>
    

    【讨论】:

    • 谢谢。那个问题就解决了。但另一个问题发生了。工具栏位于状态栏高度。
    • 尝试将fitsSytemWindows=true也放入工具栏中
    • 尽管在工具栏中放置了 fitSytemWindows=true ,但我遇到了同样的问题。 ㅠㅠ
    • 试着把这个app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed" 放到 CollapsingToolbarLayout 中
    • layout_scrollFlags 无效。我大致将其解析为 android:layout_marginTop="16dp" android:layout_marginBottom="8dp"。无论如何,谢谢。
    【解决方案3】:

    将您的TabLayout 移到CollapsingToolbarLayout 之外,但仍在AppBarLayout 内部。像这样

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-17
      • 2015-06-25
      • 1970-01-01
      • 2012-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多