使用tabhost中的每个选项卡设置axml布局:Xamarin Android

时间:2013-12-17 11:20:38

标签: c# android xml xamarin

如何为每个标签设置xml布局?

创建每个标签的代码

 private void CreateTab(Type activityType, string tag, string label, int drawableId)
    {
        TabHost.TabSpec spec;

        var intent = new Intent(this, activityType);
        intent.AddFlags(ActivityFlags.NewTask);


        spec = TabHost.NewTabSpec(tag);

        var drawableIcon = Resources.GetDrawable(drawableId);
        spec.SetIndicator("", drawableIcon);

        spec.SetContent(intent);

        TabHost.AddTab(spec);
    } 

以下是第一个标签活动的代码

[Activity(Label = "My Activity")]
public class WhatsOnActivity : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        //TextView textview = new TextView(this);   // working 
        //textview.Text = "This is Whats on activity tab";
        //SetContentView(textview);

        SetContentView(Resource.Layout.Feed); // not working
    }
}

触发异常。我怎样才能使它发挥作用?

请帮忙, 谢谢

1 个答案:

答案 0 :(得分:0)

希望这个问题仍然有效。 请找到对tabhost的引用:TabHost Walkthrough

Main.axml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TabHost android:id="@+id/tabhost_sample"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TabWidget android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>

        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp">

            <LinearLayout android:id="@+id/tab_sampletab1"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
            />

            <LinearLayout android:id="@+id/tab_sampletab2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
            />

        </FrameLayout>
     </TabHost>

</RelativeLayout>

MainActivity.cs:

[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);

        TabHost tabHost = FindViewById(Resource.Id.tabhost_sample) as TabHost;
        tabHost.Setup();

        TabHost.TabSpec tabSpec1 = tabHost.NewTabSpec("SampleTab1");
        tabSpec1.SetContent(Resource.Id.tab_sampletab1);
        tabSpec1.SetIndicator("SampleTab1");

        TabHost.TabSpec tabSpec2 = tabHost.NewTabSpec("SampleTab2");
        tabSpec2.SetContent(Resource.Id.tab_sampletab2);
        tabSpec2.SetIndicator("SampleTab2");

        tabHost.AddTab(tabSpec1);
        tabHost.AddTab(tabSpec2);
    }
}