片段底部的选项卡

时间:2016-05-03 00:52:40

标签: android android-fragments android-tabs fragment-tab-host

我有一个来自母亲活动的Fragment,我想在其中使用FragmentTabHost显示两个标签。但我希望标签位于片段底部而不是顶部,因为它是默认值。

MainFragment.java:

    public class MainFragment extends Fragment {

    private FragmentTabHost tabHost;
    private View rootView;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

        //tabHost = new FragmentTabHost(getActivity());
        rootView = inflater.inflate(R.layout.fragment_main, container, false);

        tabHost = (FragmentTabHost) rootView.findViewById(R.id.tabhost);

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

        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab 1"), ConnectFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Tab 2"), ManageFragment.class, null);

        tabHost.setCurrentTab(0);

        return tabHost;

    }
}

我使用的布局非常基本,当我运行此版本时,一切正常,但标签显示在片段的顶部。

fragment_main.xml:

<android.support.v4.app.FragmentTabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/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" >

        <FrameLayout
            android:id="@+id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <TabWidget
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"/>
    </LinearLayout>

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

当我运行一个稍微不同的版本时,应该将标签放在片段的底部 - 根据本网站的其他线程 - 我收到错误。

fragment_main_v2.xml:

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

    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" />

    <android.support.v4.app.FragmentTabHost
        android:id="@+id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <FrameLayout
            android:id="@+id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />
    </android.support.v4.app.FragmentTabHost>

</LinearLayout>

抛出错误:

  

android.view.InflateException:指定的子节点已有父节点。您必须先在孩子的父母身上调用removeView()。

我可以想象

出了问题
  

android.view.InflateException:指定的子节点已有父节点。您必须先在孩子的父母身上调用removeView()。

0 个答案:

没有答案
相关问题