在扩展片段的 Class 中使用 FragmentTabHost 的水平可滚动选项卡

Horizontal Scrollable Tabs Using FragmentTabHost In A Class That Extends Fragment

我正在使用扩展 Fragment 而不是 FragmentActivity 的 class,我似乎无法使水平滚动起作用。我首先使用了一个像下面第一个代码一样简单的布局,它工作得很好(当然没有滚动)。我决定放置一个水平滚动条,因为当我的标签达到 6 时,文本很难阅读,当文本太长时,无法写入完整的文本。我尝试使用其他人说的 "This works!" 的布局。请参考下面第二个代码;第三个代码是我的片段class。谢谢!

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

别人说的布局"It works"

<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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <HorizontalScrollView
            android:layout_width="match_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"
                android:layout_gravity="bottom" />

        </HorizontalScrollView>

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

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

最后,我的 Fragment class。也许我遗漏了什么?

public class AlarmTab extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        FragmentTabHost tabHost;
        tabHost = new FragmentTabHost(getActivity());
        tabHost.setup(getActivity(), getChildFragmentManager(), R.layout.alarm_tab);


        for(int i=0; i<SmashDeviceData.get_instance().devices.size(); i++) {
            Bundle bundle = new Bundle();
            bundle.putInt("Arg for Frag" + i, 1);
            tabHost.addTab(tabHost.newTabSpec("Tab"+i).setIndicator(SmashDeviceData.get_instance().devices.get(i).deviceName), AlarmActivity.class, bundle);
        }
            return tabHost;
    }


    public static Fragment newInstance() {
        Fragment fragment = new AlarmTab();
        return fragment;
    }
}

试试下面的方法

对于xml

<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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical" >


            <HorizontalScrollView
                android:id="@+id/horizontalScrollView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@color/tabsColor"
                android:scrollbars="none" >

                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@color/tabsColor"
                    android:gravity="center_horizontal" />
            </HorizontalScrollView>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>

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

为你的片段

public class HomeFragment extends Fragment {

    private FragmentTabHost mTabHost;

    TabWidget widget;
    HorizontalScrollView hs;

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

        initView(rootView);

        return rootView;
    }


    private void initView(View rootView) {
        mTabHost = (FragmentTabHost) rootView
                .findViewById(android.R.id.tabhost);

        widget = (TabWidget) rootView.findViewById(android.R.id.tabs);
        hs = (HorizontalScrollView) rootView
                .findViewById(R.id.horizontalScrollView);


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

}