android - 如何使用带有 fragment 的 Android AppBarLayout、Toolbar 和 TabLayout

标签 android android-fragments android-design-library androiddesignsupport

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

对于这个问题我看不到几个解决方案:

  • 禁用 Toolbar 和 TabLaout 的提升(不喜欢)
  • 从 Activity 中删除工具栏并将其移动到 fragment 中

您知道如何在我的场景中正确使用它吗?

Wrong drop shadow

最佳答案

我遇到了同样的问题,很容易解决。 只需从 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/

相关文章:

安卓工作室 3.1 : updating gradle file cause android studio to freeze

android - RecyclerView.Adapter 的 onCreateViewHolder 方法中的 parent.getContext

android - fragment 权限

android float 操作按钮动态更改 View anchor

android - 在 Android 上使用设计支持库切换抽屉导航项目

android - 如何在没有 USB 电缆的情况下通过 wifi 网络连接 ADB?

android - 当我添加安全参数插件时,Gradle 同步失败

java - 我有这个错误 :Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference

android - 如何通过单击选项菜单更改 MainActivity 中 webview 的 url

android - 编辑文本中的密码可见性切换无法正常工作