0

我想顯示自定義工具欄,並顯示家庭(漢堡包)圖標和後退箭頭圖標,當我導航到其他片段。自定義android工具欄後退按鈕不顯示

顯示主頁按鈕,但後退箭頭不是。

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

回答

0

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

在片段的宿主類,我處理該事件中,當片段被改變,並且取決於片段的類型,在工具欄上顯示回或主頁按鈕:

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