【问题标题】:Android TabHost inside TabHost's tabTabHost 选项卡内的 Android TabHost
【发布时间】:2017-02-15 11:19:33
【问题描述】:

问候我的开发人员。 我正在开发一个使用 TabHost 的 android 项目,并且在第二个选项卡内我想使用另一个 TabHost, 到目前为止我已经完成了这个 我的主要 TabHost XML 是

<TabHost
    android:id="@+id/homeTabHost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:orientation="vertical">

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

                <!--first tab content-->
                <include layout="@layout/content_blood_glucose_diary_home" />

                <!--second tab content-->
                <include layout="@layout/content_blood_glucose_diary_summary" />

                <!--3rd tab content-->
                <include layout="@layout/content_blood_glucose_diary_activities" />

            </FrameLayout>
        </LinearLayout>

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

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

我的java代码是

private void loadTabs(int tabhost, String tab1, String tab2, String tab3){
    TabHost host = (TabHost)findViewById(tabhost);
    host.setup();

    loadTab(host, R.id.homeTab1, tab1); //tab 1

    loadTab(host, R.id.homeTab2, tab2); //tab 2

    loadTab(host, R.id.homeTab3, tab3); //tab 3
}

private void loadTab(TabHost host, int tabid, String tab){
    TabHost.TabSpec spec = host.newTabSpec(tab);
    spec.setContent(tabid);
    spec.setIndicator(tab);
    host.addTab(spec);
}

我从我的 onCreate 调用它

loadTabs(R.id.homeTabHost, "Today", "Summary", "Activity");

在这一点上,事情变得更好 但是当在我的摘要 xml 中添加另一个 tabhost 时(像这样)

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal"
    android:weightSum="1">

    <TabHost
        android:id="@+id/summaryTabHost"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="0.8">

        <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">

                <!--first tab content-->
                <LinearLayout
                    android:id="@+id/summaryTab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="tab1" />
                </LinearLayout>

                <!--second tab content-->
                <LinearLayout
                    android:id="@+id/summaryTab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="tab2" />
                </LinearLayout>

                <!--3rd tab content-->
                <LinearLayout
                    android:id="@+id/summaryTab3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="tab3" />
                </LinearLayout>

                <!--4th tab content-->
                <LinearLayout
                    android:id="@+id/summaryTab4"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="tab4" />
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>
</LinearLayout>

并在我的 java 文件中添加

private void loadTabs(int tabhost, String tab1, String tab2, String tab3, String tab4){
    TabHost host = (TabHost)findViewById(tabhost);
    host.setup();

    loadTab(host, R.id.summaryTab1, tab1); //tab 1

    loadTab(host, R.id.summaryTab2, tab2); //tab 2

    loadTab(host, R.id.summaryTab3, tab3); //tab 3

    loadTab(host, R.id.summaryTab4, tab4); //tab 4
}

这在我的 oncreate 函数中是

loadTabs(R.id.summaryTabHost, "All", "Yearly", "Monthly", "Weekly");

这应该可以... 因为它与第一个标签主机相同.. 第一个标签主机标签不显示是的..

但是当我在摘要 xml 中评论 2nd tabhost.. 事情看起来很正常。

到目前为止,我的诊断是第二个 TabHost 可能有一些问题。

任何想法这里可能有什么问题???任何帮助都得到承认!

【问题讨论】:

  • 在此示例中...我在屏幕底部创建了 tabhost,但如果我将其设置在屏幕顶部,则两个 tabhost 都可以正常工作。如果我在屏幕顶部创建第一个 tabhost,它也可以正常工作和屏幕的内部 tabhost 底部它也可以工作.. 似乎第一个 tabhost 只有在我尝试按照我的问题做时才会消失

标签: java android xml android-tabhost


【解决方案1】:

TabHost 已弃用,您可以使用 tablayout 如果你想在标签内制作标签,你可以在内部片段中轻松使用它

Androidhive tablayout tutorial

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多