Android FragmentTabHost在选项卡中显示内容
我正在尝试设置一个带有3个选项卡的FragmentTabHost。我的问题是每个标签的内容实际上也显示在标签标题中。为什么会这样?tab1的布局有一个文本视图,上面写着“tab1”,它显示在选项卡中,而不是内容区域。此外,如果我更改背景,它会更改选项卡的背景,而不是内容区域 xml:Android FragmentTabHost在选项卡中显示内容,android,android-fragments,android-tabhost,Android,Android Fragments,Android Tabhost,我正在尝试设置一个带有3个选项卡的FragmentTabHost。我的问题是每个标签的内容实际上也显示在标签标题中。为什么会这样?tab1的布局有一个文本视图,上面写着“tab1”,它显示在选项卡中,而不是内容区域。此外,如果我更改背景,它会更改选项卡的背景,而不是内容区域 xml: 首先,假设您的XML名称是my_XML.XML。 因此,首先在my_xml.xml中进行以下更改(更改ID) 你错过了这两个: <TabWidget android:id="@android
首先,假设您的XML名称是my_XML.XML。 因此,首先在my_xml.xml中进行以下更改(更改ID) 你错过了这两个:
<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"/>
因此,请将它们添加到线性布局
上方的框架布局
实际上,您不需要任何更改,因为您的代码与演示完全相同,请参见以下内容:
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;
}
<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" >
mTabHost = (FragmentTabHost)v.findViewById(R.id.my_tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.my_xml);
<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"/>