【问题标题】:Android TabHost only inside one Fragment (NavigationDrawer)Android TabHost 仅在一个 Fragment 内(NavigationDrawer)
【发布时间】:2013-12-30 19:41:07
【问题描述】:

我目前正在编写一个应用程序,该应用程序使用大量可通过导航抽屉访问的片段。到目前为止一切顺利,但我也希望在其中一个片段中有一个带有 2 个选项卡的 TabHost。我如何最好地实施它?这是一个代码sn-p:

public class SectionFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

int position = getArguments().getInt("position");

if (position == 0) {

    rootView = inflater.inflate(R.layout.startmenu_layout, container,
        false); // die rootView zum Weiterarbeiten holen
} else if (position == 1) {
    rootView = inflater.inflate(R.layout.startmenu_layout, container,
        false);

等等……

我怎样才能最好地进行? 提前致谢,

论坛发帖人

【问题讨论】:

  • 到目前为止你是如何尝试实现它的?
  • @Luksprog 我尝试调用 2 个内部片段(我认为这是您应该这样做的方式)并使用 TabHost 使它们可访问,但我不知道如何正确编码。
  • 您想做的事情并没有什么特别之处。您将使用与 getChildFragmentManager() 而不是 getFragmentManager()(或支持版本)相同的代码。另外,你可以看看FragmentTabHost

标签: android android-fragments navigation-drawer


【解决方案1】:

这是我用于 TabHost 的代码 我知道你的感受,因为很难找到一个有效的教程和一个有效的例子......

不管怎样

TabHost tabs=(TabHost)findViewById(R.id.tabhost);

tabs.setup();

TabHost.TabSpec spec=tabs.newTabSpec("tag1");

spec.setContent(R.id.tab1);//here you define which tab you want to setup
spec.setIndicator("So Close");//here you choose the text showed in the tab
tabs.addTab(spec);

spec=tabs.newTabSpec("tag2");
spec.setContent(R.id.tab2);
spec.setIndicator("Contacts");
tabs.addTab(spec);

这是我使用的xml代码

<TabHost
        android:id="@+id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/setting" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="46dp"
            android:background="@drawable/transparent_white" />

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

            <TextView
                android:id="@+id/tab1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textColor="@color/white"
                android:textAppearance="?android:attr/textAppearanceLarge" />


            <TextView
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:textAppearance="?android:attr/textAppearanceLarge"  />

        </FrameLayout>

    </LinearLayout>
</TabHost>

确保根据您的 res 更改 TabWidget 的背景

您可以将 xml 代码放在布局文件中的任何位置,RelativeLayout 或 LinearLayout...您可以更改选项卡中显示的数据类型...希望我能提供帮助 :)

【讨论】:

  • 该死,这是一个简单但有效的解决方案!多么无价的一段代码!非常感谢!
  • 我仍然不明白这是如何在使用导航抽屉附加到 Activity 的片段中工作的。很少有这样的例子被使用。只有我的一个片段需要在窗口顶部保留选项卡。
猜你喜欢
  • 2011-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多