【问题标题】:Horizontal Scrollable Tabs Using FragmentTabHost In A Class That Extends Fragment在扩展 Fragment 的类中使用 FragmentTabHost 的水平滚动选项卡
【发布时间】:2015-07-01 11:17:54
【问题描述】:

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

<?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"/>

其他人说“有效”的布局

<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 班级。也许我错过了什么?

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

【问题讨论】:

    标签: android android-fragments android-tabs


    【解决方案1】:

    试试下面的

    对于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);
    }
    
    }
    

    【讨论】:

    • 效果很好!谢谢!你救了我的命! @Ahmed Basyouny
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    • 1970-01-01
    • 2014-01-19
    • 1970-01-01
    相关资源
    最近更新 更多