我的开发环境是安卓0.8.9工作室与API19 SDK。
如果我把FragmentTabHost放在FragmentActivity中,它就可以工作。 当我把FragmentTabHost放在一个片段中时,它在渲染时会得到“没有标签为空的标签”,并在LayoutInflater膨胀布局时得到运行时错误。
感谢user3216049的回答,这是一个很好的解决方法。 对不起,我不能投票,因为我是一个新手。 :(
但是它没有显示在我的测试标签片段。 我做了一个小的修改。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
</LinearLayout>
- fragment_section_dummy。XML
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="24sp"
android:padding="32dp" />
- Java代码 的一点是,我改变ID在FragmentTabHost.setup了 “R.id.realtabcontent”()
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
public class TestFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
FragmentTabHost tabHost = new FragmentTabHost(getActivity());
inflater.inflate(R.layout.test_fragment, tabHost);
tabHost.setup(getActivity(),
getChildFragmentManager(), R.id.realtabcontent);
tabHost.addTab(tabHost.newTabSpec("simple")
.setIndicator("Simple"), DummySectionFragment.class, null);
tabHost.addTab(tabHost.newTabSpec("contacts")
.setIndicator("Contacts"), DummySectionFragment.class, null);
return tabHost;
}
/**
* A dummy fragment representing a section of the app,
* but that simply displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
private static int count = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_section_dummy,
container, false);
((TextView) rootView.findViewById(android.R.id.text1))
.setText("Dummy Section " + count++);
return rootView;
}
}
}
你使用什么代码?你在使用菜单xml吗?你有一个logcat吗? – 2013-07-04 05:58:27
你可以发布初始化TabHost的代码吗? – 2013-07-06 04:40:46
我打开了一个错误报告,请对其进行投票:https://code.google.com/p/android/issues/detail?id=78772 – Gavriel 2014-11-05 21:37:31