Tab host not showing tabs

喜你入骨 提交于 2019-12-08 06:41:02

问题


I am using tab host to show tabs, but even when I simply drag and drop it on my layout, and run it, its not showing the tabs, its showing white screen,I guess which is of the linear layout of first tab view.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TabHost
        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" >
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                </LinearLayout>

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

</LinearLayout>

When I run above on emulator or phone (Huawei Ascend P1), no tabs are shown.


回答1:


This is happening simply because you just can't create TabHost using xml code only. You need to add TabSpecs to the TabHost like this:

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);



回答2:


This answer is for the people, like myself, who started using Kotlin for Android development. The following code used three ids that you need to match it to your view's XML file. First as stated in the library:

Call setup() before adding tabs if loading TabHost using findViewById(). However: You do not need to call setup() after getTabHost() in TabActivity.

    //Your tab host id. 
    yourTabHostId.setup()

    //Need to use var as we will reassign it for the second tab.
    //First tab is a tag and must be defined.
    var tabSpec = yourTabHostId.newTabSpec("First Tab")
    //Use the id for the first tab, for the content of your first tab.
    tabSpec.setContent(R.id.firstTab)
    tabSpec.setIndicator("First Tab")
    yourTabHostId.addTab(tabSpec)

    tabSpec = yourTabHostId.newTabSpec("Second Tab")
    //Use the id for your second tab, for the content of your second tab.
    tabSpec.setContent(R.id.secondTab)
    tabSpec.setIndicator("Second Tab")
    yourTabHostId.addTab(tabSpec)


来源:https://stackoverflow.com/questions/17018660/tab-host-not-showing-tabs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!