如何使用Android AppBarLayout,工具栏和TabLayout与片段

rad*_*zio 5 android android-fragments android-design-library androiddesignsupport

我有使用Toolbar的NavigationDrawer和AppBarLayout的活动.不幸的是,当我在子片段中使用TabLayout时,我看到工具栏和TabLayout之间的阴影.是否可以在TabLayout下方显示阴影?我不想将TabLayout移动到我的活动中,因为我只在一个片段中使用它.

我可以看到这个问题的解决方案:

  • 禁用工具栏和TabLaout上的高程(不喜欢它)
  • 从活动中删除工具栏并将其移动到片段中

您有什么想法如何在我的场景中正确使用它?

错误的投影

FOM*_*per 2

我有同样的问题,而且很容易解决。只需从 Activity 中删除 AppBarLayout 并将其放入片段中即可。通过这样做,您可以将工具栏保留在活动中,并将带有阴影的 TabLayout 保留在片段中。

活动:

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <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" />


    <FrameLayout
        android:id="@+id/main_activity_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>


</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

分段:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">


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

        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

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

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v4.view.ViewPager>


</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

希望有帮助!