【问题标题】:How to fix a gap between AppBarlayout and TabLayout如何修复 AppBarlayout 和 TabLayout 之间的差距
【发布时间】:2019-06-29 18:50:19
【问题描述】:

我正在尝试将 AppBarLayout 与 Toolbar、TabLayout 组件和 Viewpager 一起使用,但我在工具栏和 tablayout 之间发现了一个黑色间隙,并且不确定为什么。我查看了这里的一些示例,但它们不起作用。

我把我的问题的图片。

XML

<RelativeLayout
    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:background="@color/white">

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

    <android.support.design.widget.AppBarLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/home_appbarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="0dp">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            android:background="@color/white"
            app:contentInsetStart="@dimen/margin_8"
            app:contentInsetStartWithNavigation="@dimen/margin_8"
            app:layout_collapseMode="pin"
            app:paddingStart="0dp"
            app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:titleMarginStart="@dimen/margin_8">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="toolbar"/>
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

        <android.support.design.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/rosa_apelucy">
            <android.support.design.widget.TabLayout
                android:id="@+id/tab_layout"
                app:tabMode="fixed"
                android:layout_width="match_parent"
                android:layout_height="?android:attr/actionBarSize"
                android:background="@color/transparent"
                android:elevation="6dp"
                app:tabTextColor="@color/grey"
                app:tabSelectedTextColor="@color/pink"
                app:tabIndicatorColor="@color/pink"/>

            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/white"/>
        </android.support.design.widget.CoordinatorLayout>
    </LinearLayout>
</RelativeLayout>

更新问题

<android.support.design.widget.CoordinatorLayout
        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:background="@color/blanco">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/home_appbarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="0dp">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            app:contentInsetStart="@dimen/margin_8"
            app:contentInsetStartWithNavigation="@dimen/margin_8"
            app:layout_collapseMode="pin"
            app:paddingStart="0dp"
            app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:titleMarginStart="@dimen/margin_8">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="toolbar"/>
        </android.support.v7.widget.Toolbar>
        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            app:tabMode="fixed"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/transparent"
            android:elevation="6dp"
            android:minHeight="?android:attr/actionBarSize"
            app:tabTextColor="@color/gris_oscuro"
            app:tabSelectedTextColor="@color/rosa_apelucy"
            app:tabIndicatorColor="@color/rosa_apelucy"
            android:layout_marginTop="@dimen/margin_25"/>
    </android.support.design.widget.AppBarLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            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-viewpager android-toolbar android-tablayout


    【解决方案1】:

    在我看来,这似乎不是一个好的实践设计。您在其中使用了 CoordinatorLayoutTabLayout 以及更多问题。

    相反,使用CoordinatorLayout 作为根布局,在AppBarLayout 之后作为子布局,然后在AppBarLayout 中使用ToolbarTabLayout

    因此,结构将是:

    <CoordinatorLayout
      <AppBarLayout
       <Toolbar/>
        <TabLayout/> 
      </AppBarLayout>
       <ViewPager/>
    <CoordinatorLayout/>
    

    之后,像这样在CoordinatorLayout 中使用ViewPager 并添加以下代码行:

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

    更新:支持库:

    <android.support.design.widget.CoordinatorLayout 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/home_appbarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:elevation="0dp">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?android:attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:theme="@style/ThemeOverlay.AppCompat.ActionBar">
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="toolbar" />
            </android.support.v7.widget.Toolbar>
    
            <android.support.design.widget.TabLayout
                android:id="@+id/tab_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:elevation="6dp"
                android:minHeight="?android:attr/actionBarSize"
                app:tabMode="fixed" />
        </android.support.design.widget.AppBarLayout>
    
        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    </android.support.design.widget.CoordinatorLayout>
    

    工作布局 (AndroidX)

    <androidx.coordinatorlayout.widget.CoordinatorLayout 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">
    
        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/home_appbarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:elevation="0dp">
    
            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?android:attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:theme="@style/ThemeOverlay.AppCompat.ActionBar">
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="toolbar" />
            </androidx.appcompat.widget.Toolbar>
    
            <com.google.android.material.tabs.TabLayout
                android:id="@+id/tab_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:elevation="6dp"
                android:minHeight="?android:attr/actionBarSize"
                app:tabMode="fixed" />
        </com.google.android.material.appbar.AppBarLayout>
    
        <androidx.viewpager.widget.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

    【讨论】:

    • 如果他将这个 RelativeLayout 保留为黑色背景,他可以做任何他想做的事情,他总是会看到这个黑色的 25dp 高度线。
    • 不是问题。我已经删除它并保持差距
    • @MaikPeschutter 如果他/她这样使用ViewPager,它将无法正常工作。实际上,提到了这样做的正确方法。
    • @S.P.我并没有完全说“删除它”,问题出在代码中的基本设计中。请再看看我的回答。您设计的方式似乎不正确。为您提供了实际实现此类布局的最佳方法。
    • 我试过了,但是没有显示工具栏,也没有显示tablayout。请用您的代码检查我更新问题
    【解决方案2】:

    删除

    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="25dp"
            android:background="@color/black">
    </RelativeLayout>
    

    【讨论】:

    • 这部分不是问题...我删除了仍然存在差距
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 2017-05-02
    • 1970-01-01
    • 1970-01-01
    • 2016-02-18
    相关资源
    最近更新 更多