在导航视图中显示和同步汉堡包图标?

时间:2016-01-21 11:41:44

标签: android android-fragments android-toolbar

这是我的activity_main.xml。它有一个导航视图和FrameLayout。

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

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/frame_container">

        <include layout="@layout/app_bar_main" android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </FrameLayout>

    <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:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" />

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

以下代码来自MainActivity.class

 DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

处理单击导航抽屉项目:

 public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_home) {
            // Handle the camera action
        } else if (id == R.id.nav_updates) {

            UpdatesFragment fragment = new UpdatesFragment();
            android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame_container,fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
      } .. and so on

UpdatesFragment有一个视图和工具栏。我的意思是我正在为每个片段重复工具栏。

onCreateView的{​​{1}}方法中,我使用此代码显示我为UpdatesFragment专门编写的工具栏。

UpdatesFragment

我的问题是:

  1. 显示工具栏,但没有汉堡包图标。
  2. 我想要一个汉堡包图标,如果我按下,它应该按照预期打开导航抽屉。
  3. 注意:我再次重复我没有使用常用工具栏。当我做碎片事务时,我替换包括工具栏在内的所有内容每个片段都有工具栏。工具栏正在显示。只有汉堡包图标及其动作缺失。

0 个答案:

没有答案