2014-10-03 3 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(), говоря, что у указанного дочернего элемента уже есть родитель. Если я удалю LinearLayout вокруг ImageView на вкладке пользовательского вида, это сработает! Но мне нужны некоторые настройки, поэтому мне нужен LinearLayout. Что я делаю не так?

+0

пожалуйста, напишите Ваше расположение 'fragment_friends' – mmlooloo

ответ

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

+0

В любом случае, я попытался с тем, что вы сказали, но он по-прежнему бросает то же исключение. – edoardotognoni

+0

снова посмотрите => friends.setIndicator (** tab **); – mmlooloo

Смежные вопросы