Android TabHost only inside one Fragment (NavigationDrawer)

雨燕双飞 提交于 2019-12-04 20:24:09

here is a code i use for my TabHost i know how you feel cause it's really hard to find a working tutorial and a working example out there...

anyway

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

and here is the xml code I use

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

make sure you change the background of the TabWidget according to your res

you can put the xml code wherever you want in your layout file, a RelativeLayout or a LinearLayout... and you can change the type of the data shown in the tabs... Hope I could help :)

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