Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – TabHost没有显示在屏幕上大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试在我的应用程序上使用TabHost,我只是将它拖到我的活动中使用该设计,但是当我运行它时,它就不会出现,只是得到白色屏幕,有人知道为什么吗?
<TabHost
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tabHost"
    android:layout_gravity="center_horizontal">

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

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

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

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"></LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"></LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"></LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>

解决方法

发生这种情况的原因很简单,因为您只能使用XML代码创建TabHost.您需要将TabSpec添加到TabHost,如下所示:
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);

TabSpec tab1 = tabHost.newTabSpec("First Tab");
TabSpec tab2 = tabHost.newTabSpec("Second Tab");
TabSpec tab3 = tabHost.newTabSpec("Third Tab");

tab1.setIndicator("Tab1");
tab1.setContent(new Intent(this,tabactivity1.class));

tab2.setIndicator("Tab2");
tab2.setContent(new Intent(this,tabactivity2.class));

tab3.setIndicator("Tab3");
tab3.setContent(new Intent(this,tabactivity3.class));

tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);

大佬总结

以上是大佬教程为你收集整理的android – TabHost没有显示在屏幕上全部内容,希望文章能够帮你解决android – TabHost没有显示在屏幕上所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: