【问题标题】:HorizonatalScrollView on TabWidged "steals" spaceTabWidget 上的水平滚动视图“窃取”空间
【发布时间】:2011-11-09 09:09:36
【问题描述】:

我在底部有一个带有自定义 TabWidgets 的 TabActivity。我希望能够添加超过 5 个标签。与动态添加“更多”按钮的菜单栏不同,选项卡只是调整了大小并且看起来很糟糕。所以我的第一次尝试是将我的 TabWidgets 包装到一个 Horizo​​ntalScrollView 中。问题是,我有一个 ListView 作为 TabContent,它的最后一项似乎隐藏在我的 TabWidgets 后面。 这是我的 TabView 的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/main_linlay_parent"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">
  <TabHost 
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <RelativeLayout
      android:id="@+id/main_tablinear"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
      <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_above="@android:id/tabs"/>
      <HorizontalScrollView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        android:scrollbars="none"
        android:layout_alignParentBottom="true">
        <TabWidget
          android:id="@android:id/tabs"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:layout_gravity="center"/>
      </HorizontalScrollView>
    </RelativeLayout>
  </TabHost>
</LinearLayout>

我在布局中做错了吗?有没有更好的方法来应用超过 4 个选项卡?虽然我的 Nexus-S 可以看到 4 个标签很好,但我认为它们在 Wildfire 上已经很难看。任何想法表示赞赏

【问题讨论】:

  • 只有在添加超过 4 个标签时才开始滚动吗?我的有 5 个标签,但它仍然将所有内容都挤在...

标签: android layout tabs horizontal-scrolling


【解决方案1】:

试试这个。它对我有用:

代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<TabHost android:layout_weight="1" 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" android:orientation="vertical">
        <HorizontalScrollView android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <LinearLayout android:layout_height="fill_parent"
                android:orientation="horizontal" android:layout_width="fill_parent">
                <TabWidget android:layout_height="wrap_content"
                    android:id="@android:id/tabs" android:isScrollContainer="true"
                    android:layout_width="fill_parent" android:scrollbars="horizontal"></TabWidget>
            </LinearLayout>
        </HorizontalScrollView>
        <FrameLayout android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:id="@android:id/tabcontent">
            <LinearLayout android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:id="@+id/tab1"></LinearLayout>
            <LinearLayout android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:id="@+id/tab2"></LinearLayout>
            <LinearLayout android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:id="@+id/tab3"></LinearLayout>
            <LinearLayout android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:id="@+id/tab4"></LinearLayout>
            <LinearLayout android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:id="@+id/tab5"></LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost></LinearLayout>

【讨论】:

  • 带有 id/tabX 的 LinearLayouts 有什么用?我使用 TabSpecs 以编程方式在 Java 中添加选项卡
  • Markis 嗨,我只是在没有这些布局的情况下尝试过,结果相同:缺少一个列表项,并且一旦我删除 Horizo​​ntalScrollView,它就会出现。
【解决方案2】:

我自己解决了我的问题!我认为其他用户看到解决方案会有所帮助:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/main_linlay_parent"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">
  <TabHost 
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <RelativeLayout
      android:id="@+id/main_tablinear"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
      <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_above="@android:id/tabs"
        android:layout_marginBottom="50dip"/>
        <!-- note: margin bottom 50 dip above: 
           its exactly the size of my custom TabWidget  -->
      <HorizontalScrollView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        android:scrollbars="none"
        android:layout_alignParentBottom="true">
        <TabWidget
          android:id="@android:id/tabs"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:layout_gravity="center"/>
      </HorizontalScrollView>
    </RelativeLayout>
  </TabHost>
</LinearLayout>

【讨论】:

    猜你喜欢
    • 2016-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多