代码之家  ›  专栏  ›  技术社区  ›  Stefan Maimon

具有不同活动的Android tabhost

  •  1
  • Stefan Maimon  · 技术社区  · 9 年前

    我已经写了两个活动,现在我希望它们在一个tabHost中(两个选项卡,每个活动在一个选项卡中)。

    我看到了一些代码,但没有一个对我有用。 这两个活动很复杂,所以我不能把它们合并到一个班级。

    我能做什么?

    谢谢

    1 回复  |  直到 9 年前
        1
  •  0
  •   Jorgesys    9 年前

    如果你能做到这一点,我的意思是同时加载多个活动,你会遇到内存问题 Fragments ,所以我建议使用 FragmentTabHost ,将您的活动转换为 碎片 并将其加载到 碎片选项卡主机 ,这是一个示例:

    <android.support.v4.app.FragmentTabHost
        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:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"/>
    
        <FrameLayout
            android:id="@android:id/tab_content1"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0"/>
        <FrameLayout
            android:id="@+id/tab_content2"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
    
    </LinearLayout>
    </android.support.v4.app.FragmentTabHost>