簡體   English   中英

自定義android工具欄后退按鈕未顯示

[英]Custom android toolbar back button not showing

當我導航到其他片段時,我想顯示自定義工具欄,並顯示主頁(漢堡)圖標和后退箭頭圖標。

顯示“主頁”按鈕,但不顯示后退箭頭。

我的應用程序使用mvvmcross,並且我具有主要的主機活動,該活動可以管理片段:

[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屬性的自定義變體和設置自定義后退按鈕,但結果相同。 我怎樣才能做到這一點?

終於找到了我的情況的解決方案:

在片段宿主類中,當片段發生更改時,我將處理事件,並根據片段類型在工具欄中顯示“后退”或“主頁”按鈕:

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();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM