如何为每个片段设置工具栏,如何处理抽屉切换和工具栏

alp*_*nso 12 android android-fragments android-support-library android-toolbar

我在我的应用程序中使用单个活动和许多片段方法

现在因为在我的一些片段中我在工具栏中有自定义视图,所以我决定为每个片段分别设置工具栏.

如何为每个片段实现单独的工具栏,抽屉布局也在我的活动中 主页 类别页面

小智 0

我不确定我是否正确理解了您对应用程序的描述,但我最近做了我认为您所描述的事情。我的活动布局是一个 DrawerLayout,其中包含一个 CoordinatorLayout/AppBar 布局,下面有一个工具栏和一个 FrameLayout。menu.xml 包含我的工具栏中所有片段所需的所有项目。在导航菜单中单击的项目将交换 FrameLayout 中的片段。我的 onNavigationItemSelected() 调用此方法来交换片段并处理返回堆栈:

 public void switchView(int id, int optArg) {
   if (currentView != id) {
     currentView = id; //currentView keeps track of which fragment is loaded
     FragmentTransaction transaction = getFragmentManager().beginTransaction();
     //Fragment contentFragment is the current fragment in the FrameLayout
     switch (id) {
       case 0: //menu item 1
         contentFragment = new Nav_Item1_Fragment();
         transaction.replace(R.id.fragment_container, contentFragment, "");
         break;
       case 1: //menu item 2
         contentFragment = new Nav_Item2_Fragment();
         transaction.replace(R.id.fragment_container, contentFragment, "");
         break;
       case 2: //menu item 3
         contentFragment = new Nav_Item3_Fragment();
         transaction.replace(R.id.fragment_container, contentFragment, "");
       break;
     }
     // Replace whatever is in the fragment_container view with this fragment,
     // and add the transaction to the back stack
     // transaction.replace(R.id.fragment_container, contentFragment);
     transaction.addToBackStack(null);
     // Commit the transaction
     transaction.commit();
   }
 }
Run Code Online (Sandbox Code Playgroud)

在每个片段的 onPrepareOptionsMenu() 中,我 setVisible() 隐藏/显示与该片段相关的工具栏中的菜单项。每个项目在活动中都有一个由菜单项的 onClick 属性指向的方法,该方法知道它来自哪个片段以及传递给它的视图。

抽屉是在活动的 onCreate() 中使用 ActionBarDrawerToggle 设置的,如下所示:

drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
        this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
Run Code Online (Sandbox Code Playgroud)

活动XML:

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    ...>

    <include
        layout="@layout/app_bar_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.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

app_bar_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.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ...>

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:gravity="top"
            .../>

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

    <FrameLayout
        android:id="@+id/fragment_container"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    </FrameLayout>

</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

所以...一个导航菜单,一个应用程序/工具栏,多个片段