自定义 android 工具栏后退按钮未显示

Custom android toolbar back button not showing

我想在导航到其他片段时显示自定义工具栏,并显示主页(汉堡包)图标和后退箭头图标。

显示主页按钮,但未显示后退箭头。

我的应用程序使用 mvvmcross,我有管理片段的主要主机 activity:

[Activity()]
public class MainView : MvxCachingFragmentCompatActivity<MainViewModel>
{
    private DrawerLayout _drawer;
    private MvxActionBarDrawerToggle _drawerToggle;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView(Resource.Layout.MainView);

        var _toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
        _toolbar.SetNavigationIcon(Resource.Drawable.back_arrow);
        SetSupportActionBar(_toolbar);
        SupportActionBar.SetDisplayHomeAsUpEnabled(true);
        SupportActionBar.SetDisplayShowHomeEnabled(true);
        SupportActionBar.SetDisplayShowTitleEnabled(false);

        _drawer = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
        _drawer.SetStatusBarBackgroundColor(Resource.Color.dark_gray);

        _drawerToggle = new MvxActionBarDrawerToggle(this, _drawer, Resource.String.open_menu, Resource.String.close_menu);

        _drawer.AddDrawerListener(_drawerToggle);
        _drawerToggle.SyncState();
    }
    //...
}

主机布局:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v4.widget.DrawerLayout
        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">
            <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">

                    <include layout="@layout/toolbar"/>

                </android.support.v7.widget.Toolbar>
            </android.support.design.widget.AppBarLayout>
            <!-- The main content view -->
            <FrameLayout
                android:id="@+id/content_frame"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>
        <!-- The navigation drawer -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:orientation="vertical">
            <FrameLayout
                android:id="@+id/menu_frame"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>
    </android.support.v4.widget.DrawerLayout>
</LinearLayout>

和工具栏布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/toolbarTitle"
            android:maxLines="1"
            android:textSize="16sp"
            android:text="My title"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:gravity="left"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp" />
    </LinearLayout>
</RelativeLayout>

所有这些我都有汉堡包图标,它没有变成后退箭头,或者当我显示其他片段时我的自定义后退箭头没有显示。 尝试使用 SupportActionBar 属性的自定义变体并设置自定义后退按钮,但结果相同。我怎样才能做到这一点?

终于为我的案例找到了解决方案:

在片段宿主 class 中,我处理事件,当片段发生变化时,根据片段类型,在工具栏中显示后退或主页按钮:

public class MainView : MvxCachingFragmentCompatActivity<MainViewModel>
{
    public override void OnAttachFragment(Fragment fragment)
    {
        base.OnAttachFragment(fragment);
        if (fragment is MainListView)
        {
            SetDrawerState(true);
        }
        else
        {
            SetDrawerState(false);
        }
    }

    public void SetDrawerState(bool isEnabled)
    {
        if (isEnabled)
        {
            _drawer.SetDrawerLockMode(DrawerLayout.LockModeUnlocked);
            _drawerToggle.OnDrawerStateChanged(DrawerLayout.LockModeUnlocked);
            _drawerToggle.DrawerIndicatorEnabled = true;
            _drawerToggle.SyncState();

        }
        else
        {
            _drawer.SetDrawerLockMode(DrawerLayout.LockModeLockedClosed);
            _drawerToggle.OnDrawerStateChanged(DrawerLayout.LockModeLockedClosed);
            _drawerToggle.DrawerIndicatorEnabled = false;
            _drawerToggle.SyncState();
        }
    }
}