2017-10-20 159 views
0

我想處理我的自定義標誌點擊ActionBar。Xamarin.Android ActionBar標誌點擊監聽器

<style name="ReadingsActionBarTheme" 
    parent="@android:style/Theme.Holo.Light.DarkActionBar"> 
<item name="android:actionBarStyle">@style/Readings.ActionBar</item> 
<item name="logo">@drawable/back</item> 
<item name="android:logo">@drawable/back</item> 

風格

我必須編譯的Android 4.4系統,這裏是我的嘗試:

public override bool OnOptionsItemSelected(Xamarin.ActionbarSherlockBinding.Views.IMenuItem item) 
    { 
     switch (item.ItemId) 
     { 
      case Android.Resource.Id.Home: 
       var homeActivity = new Intent(this, typeof(HomeActivity)); 
       StartActivity(homeActivity); ; 
       break; 
     } 
     return base.OnOptionsItemSelected(item); 
    } 

任何解決方案?

回答

1

我想處理我的自定義標誌點擊ActionBar。

你可以按照下面的步驟來處理圖標點擊ActionBar;

  1. 首先,請確保您使用Android.Support.V7.App.AppCompatActivity如果沒有,請安裝Xamarin.Android.Support.v7.AppCompat通過的NuGet和AppCompatActivity繼承您的活動。

  2. 其次,請在Resource\menu\actionbar_items.xml定義菜單項:

    <?xml version="1.0" encoding="utf-8" ?> 
    <menu xmlns:android="http://schemas.android.com/apk/res/android" 
         xmlns:app="http://schemas.android.com/apk/res-auto"> 
    
        <item 
         android:id="@+id/action_favorite" 
         android:icon="@drawable/iconName" 
         android:title="favorite" 
         app:showAsAction="ifRoom"/> 
    </menu> 
    
  3. 充氣菜單中OnCreateOptionsMenu

    public override bool OnCreateOptionsMenu(IMenu menu) 
    { 
        MenuInflater.Inflate(Resource.Menu.actionbar_items, menu); 
    
        return base.OnCreateOptionsMenu(menu); 
    } 
    
  4. OnOptionsItemSelected處理項目單擊事件:

    public override bool OnOptionsItemSelected(IMenuItem item) 
    { 
        switch (item.ItemId) 
        { 
         case Resource.Id.action_favorite: 
          //do something 
          break; 
        } 
        return base.OnOptionsItemSelected(item); 
    } 
    

下面是完整的活動:

[Activity(Label = "ActionbarClickDemo", MainLauncher = true,Theme ="@style/AppTheme")] 
public class MainActivity : AppCompatActivity 
{ 
    protected override void OnCreate(Bundle savedInstanceState) 
    { 
     base.OnCreate(savedInstanceState); 

     // Set our view from the "main" layout resource 
     SetContentView(Resource.Layout.Main); 
     Android.Support.V7.Widget.Toolbar myToolbar = (Android.Support.V7.Widget.Toolbar)FindViewById(Resource.Id.my_toolbar); 
     SetSupportActionBar(myToolbar); 
    } 

    public override bool OnCreateOptionsMenu(IMenu menu) 
    { 
     MenuInflater.Inflate(Resource.Menu.actionbar_items, menu); 

     return base.OnCreateOptionsMenu(menu); 
    } 

    public override bool OnOptionsItemSelected(IMenuItem item) 
    { 
     switch (item.ItemId) 
     { 
      case Resource.Id.action_favorite: 
       //do something 
       break; 
     } 
     return base.OnOptionsItemSelected(item); 
    } 
} 

這裏是AppTheme:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
</style> 

對於動作條的創作,請參考Setting Up the App Bar;請參考