gpt4 book ai didi

android - FragmentTabHost 不显示选项卡内容

转载 作者:行者123 更新时间:2023-11-29 21:01:19 28 4
gpt4 key购买 nike

我在程序中使用 FragmentTabHost 显示子 fragment ( fragment 中的 fragment )时遇到问题。选项卡主机显示完美,但其内容未显示...

首先介绍类:

订单.java:

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);
}

}

订单.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());
}
}

菜单列表.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 来测试它,但它也不起作用。所以,对于现在这个阶段,我的目标很简单........在 fragment 中显示选项卡内容!!!!!!

最佳答案

你有 FrameLayoutandroid:id="@android:id/tabcontent"android:layout_height="match_parent",所以它占据了所有可用的高度。但是您使用位于下方的 R.id.order_tabcontent 设置了您的 FragmentTabHost,因此您看不到它。

删除其中一个 FrameLayout 并使用另一个设置 FragmentTabHost

关于android - FragmentTabHost 不显示选项卡内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26033937/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com