0

enter image description hereim trying to set up a FragmentTabHost with 3 tabs. The problem im having is the content of each tab is actually being displayed in the tab tittle as well. Why is this happening? The layout for tab1 has a textview that says "Tab1" this is being displayed in the tab and not the content area. also if i change the background it changes the background of the tabs not the content area.

xml:

  <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
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

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

            <FrameLayout
                android:id="@+id/realtabcontent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </android.support.v4.app.FragmentTabHost>

</LinearLayout>

MAIN Fragment:

public class NewFragment _Frag extends Fragment{
    private FragmentTabHost mTabHost;



    Intent intent;
  boolean created = false;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        //return super.onCreateView(inflater, container, savedInstanceState);
        if(container==null)
        {
            return null;

        }
          View v = inflater.inflate(R.layout.mylayout, container, false);

          mTabHost = (FragmentTabHost)v.findViewById(android.R.id.tabhost);
            mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);



            mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("Tab1"),
                      myclass1.class, null);



            mTabHost.addTab(mTabHost.newTabSpec("fragmentc").setIndicator("Tab2"),
                    myclass2.class, null);
            mTabHost.addTab(mTabHost.newTabSpec("fragmentd").setIndicator("Tab3"),
                    myclass3.class, null);




          return v;
    }

2 Answers2

2

First, lets assume your XML name is my_xml.xml. So make following changes in the my_xml.xml first(Change the ID)

<android.support.v4.app.FragmentTabHost
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/my_tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

and then following changes in the java file.

mTabHost = (FragmentTabHost)v.findViewById(R.id.my_tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.my_xml);
Jagdeep Singh
  • 1,200
  • 1
  • 16
  • 34
0

you missed these two :

<TabWidget
         android:id="@android:id/tabs"
         android:orientation="horizontal"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_weight="0"/>
<FrameLayout
         android:id="@android:id/tabcontent"
         android:layout_width="0dp"
         android:layout_height="0dp"
         android:layout_weight="0"/>

so add them in your Linearlayout above framelayout

actually you do not need any changes because your code is exactly like demo see below:

https://chromium.googlesource.com/android_tools/+/18728e9dd5dd66d4f5edf1b792e77e2b544a1cb0/sdk/extras/android/support/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentTabs.java

https://chromium.googlesource.com/android_tools/+/18728e9dd5dd66d4f5edf1b792e77e2b544a1cb0/sdk/extras/android/support/samples/Support4Demos/res/layout/fragment_tabs.xml

mmlooloo
  • 18,937
  • 5
  • 45
  • 64