如何在片段中添加tabhost

我想添加tabhost里面的一个片段,但无论我尝试我不能插入它。 我可能会在这里错过一些基础知识。下面是我的类TabFragment的代码。 其中返回一个视图。

public class TabFragment extends Fragment{ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } private TabHost mTabHost; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.tabmain, container, false); mTabHost = (TabHost) view.findViewById(android.R.id.tabhost); mTabHost.setup();//very important to call this TabHost.TabSpec tab = mTabHost.newTabSpec("my tab content"); tab.setIndicator("my tab content"); mTabHost.addTab(tab); return view; } } 

Solutions Collecting From Web of "如何在片段中添加tabhost"

使用API​​级别17,现在有可能:

 import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTabHost; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; // This class is the 3rd fragment in my ViewPager, // to which I wanted to add 2 tabs.... public class TabHostParentFragment extends Fragment { private FragmentTabHost mTabHost; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mTabHost = new FragmentTabHost(getActivity()); mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.my_parent_fragment); Bundle arg1 = new Bundle(); arg1.putInt("Arg for Frag1", 1); mTabHost.addTab(mTabHost.newTabSpec("Tab1").setIndicator("Frag Tab1"), MyNestedFragment1.class, arg1); Bundle arg2 = new Bundle(); arg2.putInt("Arg for Frag2", 2); mTabHost.addTab(mTabHost.newTabSpec("Tab2").setIndicator("Frag Tab2"), MyNestedFragment2.class, arg2); return mTabHost; } @Override public void onDestroyView() { super.onDestroyView(); mTabHost = null; } } 

确保你更新了你的android-support-v4.jar文件,因为当我通过SDKpipe理器下载时,它并没有自动更新。 这可以防止定义getChildFragmentManger()函数。