android - 与 AppBarLayout 一起使用时 TabLayout 不可见?

标签 android android-tablayout android-appbarlayout

在我的应用程序中,我使用抽屉导航,我的主要 Activity 布局是这样的:

<?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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

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

app_bar_main.xml 是:

<?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:fitsSystemWindows="true">

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

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


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

    <FrameLayout
        android:id="@+id/containerView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"></FrameLayout>

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

    <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"
        app:srcCompat="@android:drawable/ic_dialog_email" />

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

tab_fragment.xml 是:

<LinearLayout 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="wrap_content"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.design.widget.TabLayout
        android:id="@id/sliding_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@color/colorPrimary"
        app:tabGravity="fill"
        app:tabMaxWidth="0.0dip"
        app:tabMode="fixed"
        app:tabTextColor="@android:color/white" />


    <android.support.v4.view.ViewPager
        android:id="@id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white" />


</LinearLayout>

一切正常,选项卡 fragment 成功加载数据,但选项卡不显示。它的选项卡似乎隐藏在工具栏/appBarLayout后面。有人可以帮我如何重新安排我的代码以显示选项卡吗?

最佳答案

FrameLayout 中添加以下属性

app:layout_behavior="@string/appbar_scrolling_view_behavior"

关于android - 与 AppBarLayout 一起使用时 TabLayout 不可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40960725/

相关文章:

android - Syncadapter 最后更新日期

java - 服务器发送消息给客户端

java - 如何将菜单设置为 AppBarLayout

android - 即使 offsetlimit 是默认值(0),ViewPager2 也会预加载下一个 fragment

android - 如何使用设计库选项卡布局更改工具栏标题?

android - 如果内容高度高于显示,工具栏内的内容不会折叠/滚动

java - 在带有后退按钮的工具栏中使用自定义标题

android - 如何在 Android 中设置自定义格式日期

android - 如何以编程方式为 LinearLayout 指定 layout_below?

android - TabLayout 中的选项卡不显示