2016-04-08 20 views
0
Main.axml

xamarin androidにアクションバータブを作成するには?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <FrameLayout 
     android:id="@+id/fragmentContainer" 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" /> 
</LinearLayout> 

そしてMainActivity.cs

protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 

      // Set our view from the "main" layout resource 
      SetContentView(Resource.Layout.Main); 

      // Get our button from the layout resource, 
      // and attach an event to it 
      //Button button = FindViewById<Button>(Resource.Id.MyButton); 

      // button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); }; 

      // enable navigation mode to support tab layout 
      this.ActionBar.NavigationMode = ActionBarNavigationMode.Tabs; 

      //adding genres tab 
      AddTab("Genres", new GenresFragment()); 
      //adding music tab 
      AddTab("Music", new MusicFragment()); 
      // 
     } 

     /* 
     tabText:title to be displayed in tab 
     iconResourceId: image/source id 
     fragment:fragment reference 
     */ 

     void AddTab(string tabText, Fragment fragment) 
     { 
      var tab = this.ActionBar.NewTab(); 
      tab.SetText(tabText); 
      // if using icon-> tab.SetIcon(iconResourceId) 

      //must set event handler for replacing tabs tab 

      tab.TabReselected += delegate (object sender, ActionBar.TabEventArgs e) 
       { 
        e.FragmentTransaction.Replace(Resource.Id.fragmentContainer, fragment); 
       }; 
      this.ActionBar.AddTab(tab); 
     } 

最初に、私は** Resource.Id.fragmentContainerがfragmentContainerの定義が含まれていないという問題を得ました。 **。

私はxamarin(visual studio 2015)の新しい開発者です。ですから、みんながこの問題を解決するのを助けてください。

答えて

2

ステップ1:SetContentView(Resource.Layout.Main)のすぐ上に、次のコードを追加します。

ActionBar.NavigationMode = ActionBarNavigationMode.Tabs; 

ステップ2:あなたは、古いデバイスをサポートする必要がある場合は、APPCOMPATライブラリが含まれており、ActionBarActivityからあなたActivityクラスを継承してくださいOnCreateの方法で

ActionBar.Tab tab = ActionBar.NewTab(); 
tab.SetText(Resources.GetString(Resource.String.tab1_text)); 
tab.SetIcon(Resource.Drawable.tab1_icon); 
tab.TabSelected += (sender, args) => { 
         // Do something when tab is selected 
        } 
ActionBar.AddTab(tab); 

tab = ActionBar.NewTab(); 
tab.SetText(Resources.GetString(Resource.String.tab2_text)); 
tab.SetIcon(Resource.Drawable.tab2_icon); 
tab.TabSelected += (sender, args) => { 
         // Do something when tab is selected 
        } 
ActionBar.AddTab(tab); 

をこれらのコードを追加します。

+0

私が作成したタブは、利用可能な幅全体を占めていません。どのように私は両側に空きスペースが残らないようにそれらを伸ばすことができますか? –

関連する問題