2013-11-28 216 views
2

我在一个片段中有三个选项卡。我想创建一个新的片段中的每个选项卡。我展示了google code。现在,我的代码可以创建标签,但不会在每个标签的内容中显示任何内容。嵌套在片段里面的片段?

我的主要片段的xml:

<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="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" 
      android:layout_weight="0" 
      android:orientation="horizontal" /> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 
    </LinearLayout> 
</android.support.v4.app.FragmentTabHost> 

如何创建的选项卡:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle  savedInstanceState) { 
    mTabHost = new FragmentTabHost(activity); 
    mTabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent); 

    mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab 1"), IconsFragment.class, null); 

    return mTabHost; 
} 

在IconsFragment,我通常充气视图正常片段。但是我没有看到这个片段中的任何项目。我如何创建嵌套片段的标签?

回答

1

问题是android.R.id.tabcontent名称与FragmentTabHost名称组件混淆。

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 
     android:id="@android:id/tabhost" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 


    </android.support.v4.app.FragmentTabHost> 

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

</LinearLayout> 

的Java

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

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

mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab 1"), GalleryMyIconsFragment.class, null); 

mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab 2"), GalleryMyIconsFragment.class, null);