Reputation: 381
In Main.axml
<?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>
And into 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);
}
The first, I got a problem that is **Resource.Id.fragmentContainer does not contain definition for fragmentContainer. **.
I'm new developer on xamarin(visual studio 2015). So please everyone help me to solve this problem.
Upvotes: 0
Views: 4963
Reputation: 7362
Step 1 : Just above SetContentView(Resource.Layout.Main);, add the following code.
ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
Step 2 : Add these code in OnCreate Method
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);
If you need to support older device, please include AppCompat library and inherit you Activity class from ActionBarActivity.
Upvotes: 2