[英]How to create action bar tab on xamarin android?
在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>
並進入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)。 所以請大家幫助我解決這個問題。
步驟1:在SetContentView(Resource.Layout.Main);上方,添加以下代碼。
ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
步驟2:在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);
如果您需要支持較舊的設備,請包括AppCompat庫,並從ActionBarActivity繼承您的Activity類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.