2014-09-25 100 views
1

我在我的程序中用FragmentTabHost显示子片段(片段中的片段)时出现问题。该选项卡主机完美显示,但其内容没有被证实...FragmentTabHost没有显示标签内容

首先,介绍一下类:

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

} 

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

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,而不是列表视图来测试它,但它也不起作用。所以,对于现在的舞台来说,我的目标非常简单........显示片段中的标签内容!!!!!!

回答

2

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

删除FrameLayout之一和设置FragmentTabHost与另一个。

+0

我删除order_tabcontent,然后它的工作原理,谢谢:) 但需要注意它也应该更改R.id.order_tabcontent到android.R.id.tabcontent安装程序() – Simon 2014-09-25 08:55:05

+0

@Simon欢迎您!我完全是这样写的:*用另一个*设置FragmentTabHost。 – 2014-09-25 09:03:40