0

我一直在研究一个由带有许多选项卡的 GUI 组成的小项目。我对片段的概念很陌生,但似乎它们是 android 选项卡环境中的最佳实践。我设法编写了一个简单的选项卡应用程序,可以在 3 个选项卡之间切换。

我遇到的唯一问题是当我尝试将选项卡放在屏幕底部时。当它们位于底部时,选项卡小部件覆盖容器(在这种情况下为框架布局),因此没有任何反应,单击各个选项卡时屏幕保持不变。

以下 XML 布局代表了良好的版本(选项卡位于屏幕顶部):

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    <TabHost
        android:id="@android:id/tabhost"
        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:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                />

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

            <FrameLayout
                android:id="@+android:id/realtabcontent"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>
        </LinearLayout>
    </TabHost>
</LinearLayout>

以下XML布局表示不起作用的版本,当标签位于屏幕底部时:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <RelativeLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            >

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

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="0dp"
                android:layout_height="0dp"
             />

            <FrameLayout
                android:id="@+android:id/realtabcontent"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
            />
        </RelativeLayout>
    </TabHost>
</LinearLayout>

我将布局更改为 relativelayout 并将此命令:android:layout_alignParentBottom="true" 添加到 tabwidget。有没有人对我如何解决这个问题有另一种想法?提前致谢。

4

2 回答 2

2

一般来说,在屏幕底部使用标签是一个糟糕的解决方案(请参阅Android 设计指南)。

对于您的解决方案:放在TabWidget视图列表的末尾。因为绘图是一个线条过程和片段,所以你的案例被淹没在上面 TabWidget

于 2012-08-06T11:36:30.373 回答
0

您可以改用重量。试试这个布局标记

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

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

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

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

您可以用自己的 TabHost 类替换 FragmentTabHost。因此,您的选项卡将位于屏幕底部。小心嵌套权重(它们对性能不利)和深入视图层次结构(特别是对于 android < 4)。

于 2014-10-21T01:15:50.507 回答