簡體   English   中英

工具欄中的后退按鈕不起作用

[英]Back button in Toolbar not working

我只是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);
            }

不幸的是,只要正確顯示了工具欄,再按任何鍵都不會有任何反應。 我要補充一點,在其他活動(使用片段)中,一切正常。

請幫我

它應該像這樣工作

public override bool OnOptionsItemSelected(IMenuItem item)
{
    //Back button pressed -> toggle event
    if (item.ItemId == Android.Resource.Id.Home)
        this.OnBackPressed(); 

    return base.OnOptionsItemSelected(item);
}

嘗試這樣的事情:

只需在您的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;
}

試試this.toolbar.setNavigationOnClickListener並根據您的需要使其處理onBackPressed或popBackstack。

嘗試這樣做:

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

暫無
暫無

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

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