Android,Tabs的内容重叠

时间:2013-11-07 09:07:57

标签: android tabs

我正在尝试为我的应用设置标签。但是,在我完成UI后,点击选项卡会导致按钮和标签同时出现。例如,Tab1显示一个按钮。当我点击Tab2时,按钮出现在Tab2上。

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TabHost
        android:id="@+id/tabhost"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="254dp"
                android:layout_height="25dp" >
            </TabWidget>

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

                <LinearLayout
                    android:id="@+id/tabPower"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical" >

                    <TextView
                        android:id="@+id/textView1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/hear" />

                    <SeekBar
                        android:id="@+id/sb"
                        android:layout_width="253dp"
                        android:layout_height="wrap_content" />

                    <ToggleButton
                        android:id="@+id/tb"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="25dp"
                        android:text="ToggleButton" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tabEq"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="right"
                        android:orientation="horizontal" >

                        <CheckBox
                            android:id="@+id/enabled"
                            android:layout_width="105dp"
                            android:layout_height="wrap_content"
                            android:layout_gravity="fill"
                            android:layout_weight="1"
                            android:text="@string/enabled" />

                        <Button
                            android:id="@+id/flat"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="left"
                            android:layout_marginRight="18dp"
                            android:layout_marginTop="15dp"
                            android:layout_weight="0.42"
                            android:text="@string/flat" />
                    </LinearLayout>

                    <TextView
                        android:id="@+id/bass_booost"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/bass_boost" />

                    <SeekBar
                        android:id="@+id/bass_boost"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="14dp"
                        android:max="1000"
                        android:progress="0" />

                    <TextView
                        android:id="@+id/textView2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.13"
                        android:text="TextView" />

                    <SeekBar
                        android:id="@+id/slider1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="14dp"
                        android:layout_weight="0.13"
                        android:max="100"
                        android:progress="50"
                        android:secondaryProgress="50" />

                    <TextView
                        android:id="@+id/textView3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.13"
                        android:text="TextView" />

                    <SeekBar
                        android:id="@+id/slider_2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="25dp"
                        android:layout_weight="0.13"
                        android:max="100"
                        android:progress="50"
                        android:secondaryProgress="50" />

                    <TextView
                        android:id="@+id/textView4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.13"
                        android:text="TextView" />

                    <SeekBar
                        android:id="@+id/slider_3"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="15dp"
                        android:layout_weight="0.13"
                        android:max="100"
                        android:progress="50"
                        android:secondaryProgress="50" />

                    <TextView
                        android:id="@+id/textView5"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.13"
                        android:text="TextView" />

                    <SeekBar
                        android:id="@+id/slider_4"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="16dp"
                        android:layout_weight="0.13"
                        android:max="100"
                        android:progress="50"
                        android:secondaryProgress="50" />

                    <TextView
                        android:id="@+id/textView6"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.13"
                        android:text="TextView" />

                    <SeekBar
                        android:id="@+id/slider_5"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="16dp"
                        android:layout_weight="0.13"
                        android:max="100"
                        android:progress="50"
                        android:secondaryProgress="50" />
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>

</LinearLayout>

这是我的布局文件,如果有什么我应该实施,请告诉我。

2 个答案:

答案 0 :(得分:5)

您可以在此处更改一些内容,使您的布局达到顶峰。首先,修改你的布局xml,注意一些事情:

  1. Android内置了tabhost元素的ID:@android:id/tabhost@android:id/tabs@android:id/tabcontent。为了简单起见,我建议使用它们;
  2. FrameLayout @android:id/tabcontent需要android:paddingTop值,否则内容会显示在标签上方。我选择了62dp(见http://androidituts.com/android-tab-layout-example/);
  3. 我已将[{1}}和android:layout_width更改为android:layout_heightmatch_parent match_parentTabHost&amp; match_parent wrap_content因为默认情况下android会为你提供一个漂亮的布局;
  4. 我删除了您TabWidget的孩子所包含的LinearLayout;没有必要;
  5. TabHostfill_parent的{​​{1}}值已弃用,因此我已将其替换为android:layout_width。请参阅http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html,其中介绍了fill_parent:
  6.   

    视图应该与其父视图一样大(减去填充)。这个常数   从API级别8开始不推荐使用,并替换为   match_parent。

    以下是新xml布局的来源:

    android:layout_height

    接下来,您必须注意在片段中设置标签。步骤很简单:

    1. 使用View.findViewById(int id)获取对TabHost的引用;
    2. 调用TabHost.setup();
    3. 将每个标签添加到TabHost。
    4. 这是设置片段视图的片段代码:

      match_parent

      就是这样!这是截图或2:

      This is the power tab

      This is the eq tab

答案 1 :(得分:0)

将XML的开头编辑为:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TabHost
        android:id="@+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" >

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

隐藏/显示您应在MainActivity

中执行的内容布局的功能