工具栏中的后退按钮不起作用
Back button in Toolbar not working
我只有 Activity,它是 ActionBarActivity class 的子项。在我设置 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);
}
遗憾的是,只要工具栏显示正确,按下回键时不再有任何反应。我要补充一点,在其他活动(使用片段)中一切正常。
请帮帮我
尝试 this.toolbar.setNavigationOnClickListener 并根据您的需要让它处理 onBackPressed 或 popBackstack。
它应该像这样工作
public override bool OnOptionsItemSelected(IMenuItem item)
{
//Back button pressed -> toggle event
if (item.ItemId == Android.Resource.Id.Home)
this.OnBackPressed();
return base.OnOptionsItemSelected(item);
}
尝试这样做:
[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);
//dont forget this
this.toolbar.SyncState();
this.toolbar += ClickedMenu;
public override bool OnOptionsItemSelected (IMenuItem item)
{
this.OnOptionsItemSelected(item);
return base.OnOptionsItemSelected (item);
}
public void ClickedMenu(object sender,SupportToolbar.MenuItemClickEventArgs e)
{
switch (e.Item.ItemId)
{ //your TitleFormatted ID
case Resource.Id.action_edit:
//do stuff here
this.OnBackPressed ();
break;
}
}
protected override void OnPostCreate(Bundle savedInstanceState)
{
base.OnPostCreate(savedInstanceState);
this.toolbar.SyncState();
}
尝试这样的事情:
只需在您的 OnCreate
方法中添加以下行:
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
然后重写 OnOptionsItemSelected
方法,如下所示。
public override bool OnOptionsItemSelected(IMenuItem item)
{
if (item.ItemId != Android.Resource.Id.Home)
return base.OnOptionsItemSelected(item);
Finish();
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home) {
// do something
}
return super.onOptionsItemSelected(item);
}
问题果然很奇怪。使用操作栏的布局具有 RelativeLayout。更改为 LinearLayout 属性后 android:gravity = "vertical",一切正常。
感谢大家的帮助
我建议您使用此代码片段在工具栏中使用自定义后退按钮:
第一步:
将图标后退按钮添加到可绘制文件夹中。
第二步:
像这样将工具栏添加到您的 AppBarLayout 中:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/chart_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
第三步:
在您的 onCreate 中找到这样的视图:
Toolbar toolbar = (Toolbar) findViewById(R.id.chart_toolbar);
第四步:
将支持操作栏添加到您的工具栏:
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
第五步:
将愿望图标添加到您的按钮:
toolbar.setNavigationIcon(ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_chevron_left));
第六步:
为您的后退按钮设置点击侦听器:
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NavUtils.navigateUpFromSameTask(Chart.this);
}
});
最后覆盖 oncreateoptionsmenu 和 onoptionsitemselected 方法:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return true;
}
我只有 Activity,它是 ActionBarActivity class 的子项。在我设置 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);
}
遗憾的是,只要工具栏显示正确,按下回键时不再有任何反应。我要补充一点,在其他活动(使用片段)中一切正常。
请帮帮我
尝试 this.toolbar.setNavigationOnClickListener 并根据您的需要让它处理 onBackPressed 或 popBackstack。
它应该像这样工作
public override bool OnOptionsItemSelected(IMenuItem item)
{
//Back button pressed -> toggle event
if (item.ItemId == Android.Resource.Id.Home)
this.OnBackPressed();
return base.OnOptionsItemSelected(item);
}
尝试这样做:
[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);
//dont forget this
this.toolbar.SyncState();
this.toolbar += ClickedMenu;
public override bool OnOptionsItemSelected (IMenuItem item)
{
this.OnOptionsItemSelected(item);
return base.OnOptionsItemSelected (item);
}
public void ClickedMenu(object sender,SupportToolbar.MenuItemClickEventArgs e)
{
switch (e.Item.ItemId)
{ //your TitleFormatted ID
case Resource.Id.action_edit:
//do stuff here
this.OnBackPressed ();
break;
}
}
protected override void OnPostCreate(Bundle savedInstanceState)
{
base.OnPostCreate(savedInstanceState);
this.toolbar.SyncState();
}
尝试这样的事情:
只需在您的 OnCreate
方法中添加以下行:
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
然后重写 OnOptionsItemSelected
方法,如下所示。
public override bool OnOptionsItemSelected(IMenuItem item)
{
if (item.ItemId != Android.Resource.Id.Home)
return base.OnOptionsItemSelected(item);
Finish();
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home) {
// do something
}
return super.onOptionsItemSelected(item);
}
问题果然很奇怪。使用操作栏的布局具有 RelativeLayout。更改为 LinearLayout 属性后 android:gravity = "vertical",一切正常。
感谢大家的帮助
我建议您使用此代码片段在工具栏中使用自定义后退按钮:
第一步: 将图标后退按钮添加到可绘制文件夹中。
第二步: 像这样将工具栏添加到您的 AppBarLayout 中:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/chart_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
第三步: 在您的 onCreate 中找到这样的视图:
Toolbar toolbar = (Toolbar) findViewById(R.id.chart_toolbar);
第四步: 将支持操作栏添加到您的工具栏:
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
第五步: 将愿望图标添加到您的按钮:
toolbar.setNavigationIcon(ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_chevron_left));
第六步: 为您的后退按钮设置点击侦听器:
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NavUtils.navigateUpFromSameTask(Chart.this);
}
});
最后覆盖 oncreateoptionsmenu 和 onoptionsitemselected 方法:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return true;
}