2014-10-03 92 views
0

我想要一個FragmentTabHost併爲選項卡設置自定義視圖。 FragmentTabHost在碎片內膨脹。這是片段代碼:FragmentTabHost:指定的孩子已經有父母

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View root = inflater.inflate(R.layout.fragment_friends, container, false); 
    mTabHost = (FragmentTabHost) root.findViewById(android.R.id.tabhost); 
    mTabHost.setup(getActivity(), getChildFragmentManager(),android.R.id.tabcontent); 

    TabHost.TabSpec friends = mTabHost.newTabSpec(FriendsTabFragment.TAG); 

    View tab = inflater.inflate(R.layout.friends_fragment_custom_tab,null); 

    ImageView view = (ImageView) tab.findViewById(R.id.tab_icon); 
    view.setImageResource(R.drawable.categoria_amici); 
    friends.setIndicator(view); 

    mTabHost.addTab(friends, FriendsTabFragment.class, null); 
    return root; 
} 

而這裏的佈局:

<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

<LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

    <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"/> 

    <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0" 
      android:layout_marginBottom="-4dp"/> 

</LinearLayout> 

</android.support.v4.app.FragmentTabHost> 

這是FriendsTabFragment充氣代碼:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View root = inflater.inflate(R.layout.fragment_friends_tab, container,false); 

    return root; 
} 

這是自定義視圖選項卡(R.layout。 friends_fragment_custom_tab):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@android:color/black"> 

<ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/tab_icon"/> 
</LinearLayout> 

如果我使用此配置運行項目,它會在mTabHost.addTab()方法上崩潰,並說明指定的子項已經有父項。如果我在自定義視圖選項卡中的ImageView周圍移除LinearLayout,它將起作用!但是我需要一些定製,所以我需要那個LinearLayout。我究竟做錯了什麼?

+0

請把你的'fragment_friends' – mmlooloo 2014-10-03 10:26:17

回答

0

佈局更改爲這一個:

<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

<LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

    <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0" 
      android:layout_marginBottom="-4dp"/> 

    <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" /> 


</LinearLayout> 

</android.support.v4.app.FragmentTabHost> 

,改變:

mTabHost.setup(getActivity(), getChildFragmentManager(),android.R.id.tabcontent); 

    friends.setIndicator(view); 

mTabHost.setup(getActivity(), getChildFragmentManager(),R.id.realtabcontent); 

    friends.setIndicator(tab); 
+0

佈局我需要的標籤控件位於佈局的底部而不是頂部。 – edoardotognoni 2014-10-03 12:07:04

+0

無論如何,我試着用你說的話,但它仍然會拋出同樣的異常。 – edoardotognoni 2014-10-03 12:12:52

+0

再看一遍=> friends.setIndicator(** tab **); – mmlooloo 2014-10-03 12:42:38

相關問題