Jak*_*ake 7 c# android toolbar xamarin
我只有Activity,它是ActionBarActivity类的子代.在我设置OnCreate的方法支持工具栏.为此,我重写OnOptionsItemSelected,所以当我按下后退按钮时执行了一些操作
代码如下所示:
[Activity (Label = "SimplyActivity", Theme="@style/MyTheme")]
public class SimplyActivity : ActionBarActivity
{
private Toolbar toolbar;
// ... OnCreate method
this.toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
SetSupportActionBar (this.toolbar);
SupportActionBar.SetDisplayHomeAsUpEnabled (true);
SupportActionBar.SetHomeButtonEnabled (true);
public override bool OnOptionsItemSelected (IMenuItem item)
{
if (item.TitleFormatted == null) this.OnBackPressed ();
return base.OnOptionsItemSelected (item);
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,只要工具栏显示正确,按下按键时就不再有任何反应.我想在其他活动(使用片段)中添加一切正常.
请帮我
小智 9
它应该像这样工作
public override bool OnOptionsItemSelected(IMenuItem item)
{
//Back button pressed -> toggle event
if (item.ItemId == Android.Resource.Id.Home)
this.OnBackPressed();
return base.OnOptionsItemSelected(item);
}
Run Code Online (Sandbox Code Playgroud)
尝试这样的事情:
只需在OnCreate方法中添加以下行:
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
Run Code Online (Sandbox Code Playgroud)
然后覆盖OnOptionsItemSelected下面给出的方法.
public override bool OnOptionsItemSelected(IMenuItem item)
{
if (item.ItemId != Android.Resource.Id.Home)
return base.OnOptionsItemSelected(item);
Finish();
return true;
}
Run Code Online (Sandbox Code Playgroud)
结果发现这个问题真的很奇怪。使用操作栏的布局有RelativeLayout。更改为 LinearLayout 属性 android:gravity = "vertical" 后,一切正常。
感谢大家的帮助
| 归档时间: |
|
| 查看次数: |
6423 次 |
| 最近记录: |