【问题标题】:how to add some Fragment into TabHost?如何在 TabHost 中添加一些片段?
【发布时间】:2017-08-15 22:06:48
【问题描述】:

我编写了一些包含 TabHost 的应用程序。 每个选项卡都包含一个 LinearLayout - 我想将我创建的一些不同的现有片段添加到每个选项卡

怎么做?

private void setNewTab(Context context, TabHost tabHost, String tag, String title, int contentID ){
    TabHost.TabSpec tabSpec = tabHost.newTabSpec(tag);

    tabSpec.setIndicator(title);


    Fragment newFragment = new ActionFragment ();


    tabSpec.setIndicator(newFragment.getView());


    tabSpec.setContent(contentID);
    tabHost.addTab(tabSpec);                     // after this line i get an exception


}




<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:id="@+id/tab1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_blue_light">

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Tab 1 Content" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/tab2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_red_dark">
            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Tab 2 Content" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/tab3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_green_light">
            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Tab 3 Content" />
        </LinearLayout>


    </FrameLayout>

</LinearLayout>

【问题讨论】:

  • 请显示您正在使用的代码
  • 附上代码
  • 你应该使用ViewPager而不是LinearLayout
  • 你想在其他 Fragment (tab) 中使用 Fragment 吗?使用 getChildFragmentManager。你仍然可以在其他片段中使用片段
  • 通常,您使用 ViewPager 设置 TabLayout 并使用 FragmentAdapter 设置 ViewPager。目前,newFragment.getView() 应该为 null,因为 Fragment 从未膨胀

标签: android android-layout


【解决方案1】:

如果有多个标签,请使用 ViewPagerFragmentStatePagerAdapter。如果您只有 2 到 3 个标签,请使用 PagerAdapter

查看下面的链接,它很好地解释了为什么要使用 ViewPagerTabLayout

https://guides.codepath.com/android/google-play-style-tabs-using-tablayout

TabLayoutViewPager 在您的场景中运行良好。

【讨论】:

    【解决方案2】:

    FragmentTabHost class 的文档中快速搜索“Android Fragment TabHost”。你在用吗?

        mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
    
      mTabHost.addTab(mTabHost.newTabSpec("action").setIndicator("action"), ActionFragment.class, null);
    

    我仍然认为 TabLayout 会是一个更简单的选择。

    如果您想要底部的标签,甚至可以选择。

    Which view should be used for new Material Design Bottom Navigation?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多