【问题标题】:FragmentTabHost: The specified child already has a parentFragmentTabHost:指定的孩子已经有一个父母
【发布时间】:2014-10-03 09:33:15
【问题描述】:

我正在尝试使用 FragmentTabHost 并为选项卡设置自定义视图。 FragmentTabHost 在 Fragment 内膨胀。这是片段代码:

 @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>

如果我使用此配置运行项目,它会在 mTab​​Host.addTab() 方法上崩溃,说明指定的子级已经有父级。如果我删除自定义视图选项卡内 ImageView 周围的 LinearLayout,它就可以工作!但我需要一些自定义,所以我需要那里的 LinearLayout。我做错了什么?

【问题讨论】:

  • 请张贴fragment_friends的布局

标签: android android-fragments android-tabhost android-tabs


【解决方案1】:

把你的布局改成这个:

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

【讨论】:

  • 我需要标签小部件位于布局的底部而不是顶部。
  • 无论如何,我按照你所说的进行了尝试,但仍然抛出相同的异常。
  • 再看一遍 => friends.setIndicator(tab);
猜你喜欢
  • 1970-01-01
  • 2017-05-21
  • 2021-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
相关资源
最近更新 更多