我有 NavigationDrawer 和 AppBarLayout with Toolbar 的 Activity 。不幸的是,当我在子 fragment 中使用 TabLayout 时,我看到工具栏和 TabLayout 之间有阴影。是否可以仅在 TabLayout 下方显示阴影?我不想将 TabLayout 移动到我的 Activity 中,因为我只在一个 fragment 中使用它。
对于这个问题我看不到几个解决方案:
- 禁用 Toolbar 和 TabLaout 的提升(不喜欢)
- 从 Activity 中删除工具栏并将其移动到 fragment 中
您知道如何在我的场景中正确使用它吗?
最佳答案
我遇到了同样的问题,很容易解决。 只需从 Activity 中删除 AppBarLayout 并将其放入 fragment 中。 通过这样做,您可以将 Toolbar 保留在 Activity 中,并将带有阴影的 TabLayout 保留在 fragment 中。
Activity :
<?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>
fragment :
<?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>
希望对您有所帮助!
关于android - 如何使用带有 fragment 的 Android AppBarLayout、Toolbar 和 TabLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32326248/