我目前正在重新设计我的应用程序以包含一个用于在应用程序中导航的抽屉导航。目前,每个屏幕都是一个 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/