I'm trying to implement an Activity with 3 tabs on it. I decided to use FragmentTabHost for this purpose. I've found some code examples with following layout file:
<?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">
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"/>
</android.support.v4.app.FragmentTabHost>
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
But I was surprised when understood that previous code sample works the same way without TabWidget:
<?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">
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.app.FragmentTabHost>
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
I just see my tabs as intended. According to the source code for FragmentTabHost, it creates TabWidget by itself if could not find it in the hierarchy of views. So I wonder if I should declare TabWidget explicitly in my layout file or not.
Well, the behavior that you describe is not documented, so there's a slight risk of regression if they drop this feature someday. Of course, having the TabWidget
child is also undocumented, since the documentation for FragmentTabHost
sucks.
You might also put the TabWidget
in your layout if you want declarative control, via XML attributes, of some aspects of the TabWidget
rendering.
Otherwise, though, if the default generated TabWidget
works for you, you are welcome to use it.