【问题标题】:Set axml layout with each tab in tabhost: Xamarin Android使用 tabhost 中的每个选项卡设置 axml 布局:Xamarin Android
【发布时间】:2014-01-05 03:11:19
【问题描述】:

如何为每个选项卡设置 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
    }
}

触发异常。我怎样才能让它工作?

请帮忙, 谢谢

【问题讨论】:

    标签: c# android xml xamarin


    【解决方案1】:

    希望这个问题仍然有效。 请参考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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多