带水平滚动的FragmentTabHost

Meh*_*hdi 8 horizontalscrollview android-fragments

我正在尝试构建FragmentTabHost并使其成为水平Scrollable.我一直在寻找解决方案,但找不到任何东西,所有帖子都是关于正常的TabHost.

我正在使用支持库,如android站点中为FragmentTabHost所解释的那样

我的布局是:

<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:tag="trip_entry_tab"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <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>
Run Code Online (Sandbox Code Playgroud)

已经尝试巢tabwidget成水平滚动视图(这是我能找到其他职位的解决方案,但始终TabHost而不是FragmentTabHost),但没有任何变化:

<HorizontalScrollView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:fillViewport="true"
            android:scrollbars="none" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
            </TabWidget>
        </HorizontalScrollView>
Run Code Online (Sandbox Code Playgroud)

我的标签刚刚收缩,看起来真的不太好看.有人让它让片段标签主机可滚动吗?

谢谢

J.N*_*nen 17

根据我的经验,FragmentTabHost不关心xml定义,必须以编程方式进行.我通过HorizontalScrollView从xml中删除它并将其添加到我的onCreatefor中来实现它FragmentActivity.

xml:

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

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" />

        <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="match_parent"
            android:layout_weight="1" />
    </LinearLayout>

</android.support.v4.app.FragmentTabHost>
Run Code Online (Sandbox Code Playgroud)

然后在添加选项卡(也以编程方式)的onCreate中:

TabWidget tw = (TabWidget) findViewById(android.R.id.tabs);
LinearLayout ll = (LinearLayout) tw.getParent();
HorizontalScrollView hs = new HorizontalScrollView(this);
hs.setLayoutParams(new FrameLayout.LayoutParams(
    FrameLayout.LayoutParams.MATCH_PARENT,
    FrameLayout.LayoutParams.WRAP_CONTENT));
ll.addView(hs, 0);
ll.removeView(tw);
hs.addView(tw);
hs.setHorizontalScrollBarEnabled(false);
Run Code Online (Sandbox Code Playgroud)

不知道这是否是最优雅的解决方案,但似乎工作正常.