当我导航到其他片段时,我想显示自定义工具栏,并显示主页(汉堡包)图标和后退箭头图标。
显示主页按钮,但后退箭头不显示。
我的应用程序使用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 :(得分: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();
}
}
}