java - 使用 fragment 在 Activity 中实现抽屉导航

标签 java android android-layout android-fragments navigation-drawer

我目前正在重新设计我的应用程序以包含一个用于在应用程序中导航的抽屉导航。目前,每个屏幕都是一个 Activity ,我设计主屏幕的方式是它包含一个工具栏和另一个 xml 文件。

<?xml version="1.0" encoding="utf-8"?>

<Toolbar
    android:layout_width="match_parent"
    android:id="@+id/toolbar"
    android:layout_height="?attr/actionBarSize"
    app:popupTheme="@style/AppTheme.PopupOverlay">
    <ImageView
        android:layout_width="@dimen/_30sdp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/_95sdp"
        android:src="@drawable/logo"
        android:id="@+id/logo"
        />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/sync_green"
        android:id="@+id/sync_button"
        android:layout_marginStart="@dimen/_75sdp"/>
</Toolbar>
<include layout="@layout/activity_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"
    android:src="@drawable/run"
   />

现在,我为抽屉导航制作了另一个 XML 文件:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- The main content view where fragments are loaded -->
    <FrameLayout
        android:id="@+id/flContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>

<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:menu="@menu/activity_main_drawer" />

我的困境是,由于我的工具栏位于 home.xml fragment 中,当我切换 fragment 时,我将丢失工具栏。我想保留抽屉导航中所有 View 的工具栏。

此外,在我的 java 代码中,setActionBar(toolbar) 抛出一个错误,指出无法解析方法 setActionBar。

最佳答案

在 Activity 中集成 NavigationDrawer 和 Fragment 的一个简单直接的解决方案是按以下方式设计 activity_main.xml:

 <?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"
    android:layoutDirection="rtl"
    tools:openDrawer="start">

    <include
        layout="@layout/content_act_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"
        android:background="@color/flat_white_light">

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

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

现在,在 content_act_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="?actionBarSize"
            android:background="?attr/colorPrimary"
            android:theme="@style/ToolbarColoredBackArrow"
            app:popupTheme="@style/AppTheme.PopupOverlay" >

        </android.support.v7.widget.Toolbar>

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

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

如果您注意到,在 conten_act_main.xml 中,有一个工具栏可供 FrameLayout 中显示的所有内容使用,您可以使用此 FrameLayout 作为您的 fragment 的容器。

关于java - 使用 fragment 在 Activity 中实现抽屉导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38181059/

相关文章:

android - 我如何处理 Android 中 Emojipicker 和键盘之间的切换?

java - 有关如何在 GWT 项目中连接到 MS SQL Server 的任何示例或教程

java - Android JNI - 字符串作为随机数返回

java - Android Settings.ACTION_DISPLAY_SETTINGS 未找到

Android - 底部工具栏 - 如何在顶部显示边框线

java - 无法弄清楚我的基本 Android 应用程序的代码有什么问题

java - 使用 help-mojo 阶段和 maven-plugin-plugin 编译 Maven 插件

java - 仅通过知道异或加密的字节数组和 key 大小来获取 key 字符串

java - 从 Tomcat 迁移到 Weblogic

java - 以编程方式确定耳机插孔的位置