带有片段的Android TABS

时间:2012-08-06 11:30:44

标签: android tabs android-fragments android-tabhost relativelayout

我一直在研究一个包含带有多个标签的GUI的小项目。我对片段的概念很新,但似乎它们是android选项卡领域中的最佳实践。我设法编写了一个简单的标签应用程序,可在3个标签之间切换。

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

以下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>

我将layour更改为relativelayout并添加了以下命令:android:layout_alignParentBottom =“true”到tabwidget。有没有人对我如何排序这个问题有另一种想法?提前谢谢。

2 个答案:

答案 0 :(得分:2)

一般情况下,使用屏幕底部的标签是一个糟糕的解决方案(参见Android design guidelines)。

对于您的解决方案:将TabWidget放在视图列表的末尾。因为绘图是一个线条过程并且碎片这个你的情况被淹没上面的TabWidget

答案 1 :(得分: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>

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