【问题标题】:I want to get my tabs scrollable, but it does not work when using a FragmentTabHost我想让我的标签可以滚动,但是在使用 FragmentTabHost 时它不起作用
【发布时间】:2013-07-04 11:18:00
【问题描述】:

我正在尝试让我的scrollable tab 工作(类似于 Google Play 中的菜单的滚动标签)。我使用android.support.v4.app.FragmentTabHost 作为标签主机,android.support.v4.app.Fragment 作为片段。除了选项卡主机不可滚动(我使用HorizontalScrollView 用于滚动功能)之外,一切正常。如果我将 tabhost 更改为标准 android.widget.TabHost 并将活动更改为 TabActivity,那么它可以工作。因此,我假设 FragmentTabHost 的某些东西使 ScrollView 无法正常工作。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

        <HorizontalScrollView android:layout_width="fill_parent"
                              android:layout_height="wrap_content"
                              android:fillViewport="true"
                              android:scrollbars="horizontal">
            <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0"
                    android:orientation="horizontal" />
        </HorizontalScrollView>

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

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

这是 FragmentTabHost 的一些代码:

public class MyTabActivity extends FragmentActivity {

private FragmentTabHost mTabHost;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.news);

    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);



    mTabHost.addTab(
            mTabHost.newTabSpec("tab1").setIndicator("Tab 1",
                    getResources().getDrawable(android.R.drawable.star_on)),
            FragmentTab.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab2").setIndicator("Tab 2",
                    getResources().getDrawable(android.R.drawable.star_on)),
            FragmentTab.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab3").setIndicator("Tab 3",
                    getResources().getDrawable(android.R.drawable.star_on)),
            FragmentTab.class, null);
    ......
    ......

如果我添加更多选项卡,选项卡会变得更小以适应屏幕。滚动条永远不会出现,但我希望它可以滚动!

怎么了?

【问题讨论】:

  • 您引用的是 ViewPagerPagerTitleStrip 而不是 FragmentTabHost
  • 不是ViewPager 用于滑动整个屏幕吗?还是可以用ViewPager滑动屏幕的一部分?
  • 您可以使用 ViewPager 滚动整个内容视图或其中的一部分。 developer.android.com/training/animation/screen-slide.html

标签: android android-fragments android-tabactivity android-scrollbar


【解决方案1】:

使用 Viewpager 是个好主意...但是...如果您想继续使用此实现,请考虑此 XML 布局。

 <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="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"/>
        </HorizontalScrollView>

【讨论】:

    猜你喜欢
    • 2014-07-12
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    • 2020-06-29
    • 2023-04-07
    • 1970-01-01
    • 2021-10-03
    • 2014-12-28
    相关资源
    最近更新 更多