1. 程式人生 > >Xamarin.Android之ActionBar與選單

Xamarin.Android之ActionBar與選單

 1     [Activity(Label = "ActionBarStudy", MainLauncher = true, Icon = "@drawable/icon")]
 2     public class MainActivity : Activity
 3     {
 4         Fragment fragment1;
 5         Fragment fragment2;
 6 
 7         protected override void OnCreate(Bundle bundle)
 8         {
 9             base
.OnCreate(bundle); 10 SetContentView(Resource.Layout.Main); 11 ActionBar.NavigationMode = ActionBarNavigationMode.Tabs; 12 13 fragment1 = new Fragment1(); 14 fragment2 = new Fragment2(); 15 16 var tab = ActionBar.NewTab(); 17 tab.SetText("
Tab1"); 18 tab.SetIcon(Resource.Drawable.Icon); 19 var tab2 = ActionBar.NewTab(); 20 tab2.SetText("Tab2"); 21 tab2.SetIcon(Resource.Drawable.Icon); 22 23 tab.TabSelected += (s, e) => 24 { 25 if (!fragment1.IsAdded)
26 { 27 e.FragmentTransaction.Add(Resource.Id.frameLayout1, fragment1); 28 } 29 if (fragment2.IsAdded && !fragment2.IsHidden) 30 { 31 e.FragmentTransaction.Hide(fragment2); 32 } 33 e.FragmentTransaction.Show(fragment1); 34 }; 35 36 37 38 tab2.TabSelected += (s, e) => 39 { 40 if (!fragment2.IsAdded) 41 { 42 e.FragmentTransaction.Add(Resource.Id.frameLayout1, fragment2); 43 } 44 if (fragment1.IsAdded && !fragment1.IsHidden) 45 { 46 e.FragmentTransaction.Hide(fragment1); 47 } 48 e.FragmentTransaction.Show(fragment2); 49 }; 50 51 ActionBar.AddTab(tab); 52 ActionBar.AddTab(tab2); 53 54 ActionBar.SelectTab(tab); 55 } 56 }