Android FragmentTabHost不显示选项卡内容
在程序中使用FragmentTabHost显示子片段(片段中的片段)时出现问题。选项卡主机显示完美,但其内容未显示 首先,介绍课程: Order.java:Android FragmentTabHost不显示选项卡内容,android,Android,在程序中使用FragmentTabHost显示子片段(片段中的片段)时出现问题。选项卡主机显示完美,但其内容未显示 首先,介绍课程: Order.java: public class Order extends Fragment{ private View view; private FragmentTabHost orderMenu; @Override public View onCreateView(LayoutInflater inflater, Vie
public class Order extends Fragment{
private View view;
private FragmentTabHost orderMenu;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
view = inflater.inflate(R.layout.order, container, false);
setLayout();
return view;
}
public void setLayout(){
orderMenu = (FragmentTabHost)view.findViewById(R.id.order_tabhost);
orderMenu.setup(getActivity(), getChildFragmentManager(), R.id.order_tabcontent);
Bundle testArg1 = new Bundle();
testArg1.putString("tag", "t1");
Bundle testArg2 = new Bundle();
testArg2.putString("tag", "t2");
Bundle testArg3 = new Bundle();
testArg3.putString("tag", "t3");
orderMenu.addTab(orderMenu.newTabSpec("t1").setIndicator("fruit"), OrderMenuList.class, testArg1);
orderMenu.addTab(orderMenu.newTabSpec("t2").setIndicator("bird"), OrderMenuList.class, testArg2);
orderMenu.addTab(orderMenu.newTabSpec("t3").setIndicator("meat"), OrderMenuList.class, testArg3);
}
}
order.xml
<?xml version="1.0" encoding="utf-8"?>
<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="@+id/order_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" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<FrameLayout
android:id="@+id/order_tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
OrderMenuList.java
public class OrderMenuList extends Fragment{
private final String TAG = this.getClass().getName();
private View view;
private ListView menuList;
private String[] menu1 = {"apple", "orange", "banana", "melon"};
private String[] menu2 = {"duck", "chicken", "turkey"};
private String[] menu3 = {"steak", "pork"};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
view = inflater.inflate(R.layout.menu_list, container, false);
Log.d(TAG, ""+this.getArguments().getString("tag"));
setLayout();
return view;
}
public void setLayout(){
menuList = (ListView)view.findViewById(R.id.menu_list);
ArrayAdapter<String> adapter = null;
if (this.getArguments()!=null){
if (this.getArguments().getString("tag").equals("t1")){
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, menu1);
} else if (this.getArguments().getString("tag").equals("t2")){
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, menu2);
} else if (this.getArguments().getString("tag").equals("t3")){
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, menu3);
}
}
menuList.setAdapter(adapter);
Log.d(TAG, ""+adapter.getCount());
}
}
公共类OrderMenuList扩展了片段{
私有最终字符串标记=this.getClass().getName();
私人视野;
私有列表视图菜单列表;
私有字符串[]menu1={“苹果”、“橘子”、“香蕉”、“甜瓜”};
私有字符串[]menu2={“鸭子”、“鸡肉”、“火鸡”};
私有字符串[]menu3={“牛排”,“猪肉”};
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图=充气机。充气(R.layout.menu\u list,container,false);
Log.d(标记“”+this.getArguments().getString(“标记”));
setLayout();
返回视图;
}
公共空间布局(){
menuList=(ListView)view.findViewById(R.id.menu\u列表);
ArrayAdapter适配器=空;
if(this.getArguments()!=null){
if(this.getArguments().getString(“标记”).equals(“t1”)){
adapter=newarrayadapter(getActivity(),android.R.layout.simple\u list\u item\u 1,menu1);
}else if(this.getArguments().getString(“标记”).equals(“t2”)){
adapter=newarrayadapter(getActivity(),android.R.layout.simple\u list\u item\u 1,menu2);
}else if(this.getArguments().getString(“标记”).equals(“t3”)){
adapter=newarrayadapter(getActivity(),android.R.layout.simple\u list\u item\u 1,menu3);
}
}
menuList.setAdapter(适配器);
Log.d(标记“”+adapter.getCount());
}
}
menu_list.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/menu_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
我也尝试过简单地使用textview而不是listview来测试它,但它也不起作用。所以,就目前阶段而言,我的目标很简单……在片段中显示选项卡内容 你有
FrameLayout
和android:id=“@android:id/tabcontent”
和android:layout\u height=“match\u parent”
,所以它占据了所有可用的高度。但是您使用驻留在下面的R.id.order\tabcontent
设置您的FragmentTabHost
,所以您看不到它
删除其中一个
FrameLayout
,然后用另一个设置FragmentTabHost
。我删除了order\u tabcontent,然后它就可以工作了,谢谢:)但需要注意的是,它还应该将R.id.order\u tabcontent更改为android.R.id.tabcontent in setup()@Simon欢迎!我写的正是:用另一个设置FragmentTabHost。