【问题标题】:How to position android tab content right under the tabs?如何在标签下定位android标签内容?
【发布时间】:2014-07-13 17:32:45
【问题描述】:

如您所见,片段内容“新文本”与选项卡小部件在同一行开始。标签内容很简单:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:layout_gravity="center_horizontal" />

</LinearLayout>

还有选项卡布局:

<android.support.v4.app.FragmentTabHost
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@android:id/tabhost">

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

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"/>

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

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

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

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

我尝试使用填充,但填充不是很安全,因为选项卡小部件的高度会随着方向的变化而变化。我也试过了

android:layout_alignParentBottom="@android:id/tabs"

在框架布局上,但是这没有效果。

如何在标签小部件/导航下正确定位标签内容?

编辑片段设置:

mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabhost);

mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab 1"),
                SomeFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab 2"),
                SomeFragment.class, null);

【问题讨论】:

  • 我看不出有什么问题。您能否发布一些代码和片段的完整 XML 布局?

标签: android android-layout


【解决方案1】:

编辑

您的问题出在 FragmentTabHost 设置方法中。请看顶部的示例 + setup(Context context, FragmentManager manager, int containerId) 方法的描述。
http://developer.android.com/reference/android/support/v4/app/FragmentTabHost.html


首先 android:layout_alignParentBottom 不使用 id 作为值,而是使用 boolean (true/false) - 它告诉是否孩子应该或不应该将其底部边界与父底部边界对齐。您可以将其设置为 true,但只有这样并不能解决您的问题。您要查找的属性是 android:layout_below="@android:id/tabs"。使用这两个属性意味着 tabcontent 视图将位于 TabWidget 的正下方(id:@android:id/tabs),并将占据整个屏幕的其余部分,直到父底部边界。

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@android:id/tabs"
        android:layout_alignParentBottom="true"
        >

顺便说一句。请使用“match_parent”而不是“fill_parent”——它的含义相同,但“fill_parent”是一个旧命名,在 Froyo(API 级别 8)中已弃用。更多信息在这里:http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#FILL_PARENT
在这里:
Is deprecated word the only difference between fill_parent and match_parent


如果您在使用 RelativeLayout 时遇到问题,请改用 LinearLayout:

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

    <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"
            android:background="@color/white"/>

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

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"/>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"/>

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

【讨论】:

  • 我刚试过这个(我遇到了完全相同的问题),但它不起作用 - 选项卡和框架布局仍然重叠。
  • 我可以确认这没有任何影响。
  • 奇怪...我添加了 LinearLayout 解决方案,它肯定会工作。
  • 不幸的是,我的结果相同。 (LinearLayout 实际上是我首先尝试的)。
  • 你能检查hierarchyview吗?请检查碎片的附着位置。如果片段附加在@android:id/tabcontent 内的任何位置,Imo 就不可能重叠任何内容。你能发布更多的代码吗?例如 FragmentTabHost 设置?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-16
  • 1970-01-01
  • 2014-02-16
  • 1970-01-01
  • 2012-02-03
  • 2016-01-09
相关资源
最近更新 更多