将 'android: layout_below' 属性 的值设置为来自不同布局 xml 文件的资源 ID

Setting the value for 'android: layout_below' property to a resource id from a different layout xml file

我有一个蓝图,看起来是这样的:

我在 xml 中指定了 tablayout 小部件,如下所示:

<android.support.design.widget.TabLayout
    android:id="@+id/tabLayout"
    style="@style/AppTabLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="@color/tabBg"
    android:minHeight="?attr/actionBarSize"
    app:tabTextColor="@color/grey"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

我设置了一个 ViewPager 滚动来为每个选项卡滑动视图。现在在其中一个视图 - 选项卡 3 中,我需要将我的组件排列在 @id/tabLayout

下方

这是tab3.xml的内容:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/secondtext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tabLayout"
        android:background="@android:color/white"
        android:text="second"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/thirdtext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/secondtext"
        android:background="@android:color/white"
        android:text="third"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

第二个和第三个textview都显示在tabLayout的后面,它们被tabLayout covered/hidden。我如何指定文本视图显示在 tabLayout 的正下方以便文本可见?

在tab3.xml中为RelativeLayout定义一个id并使用它。

与任何其他布局一样,RelativeLayout 仅负责布置其直接子级。因此,android:layout_below 和类似属性只能用于同一 RelativeLayout 父视图中的两个视图。

在这种情况下,您必须将其添加到 ViewPager。然后 TextView 就可以位于其容器的顶部,因为它现在位于 TabBar 下方。

如果这对您的其他标签没有好处。您可以执行以下操作之一:
- 将 TabBar 设置为固定高度并将其设置为文本视图的边距
- 在运行时查询 TabBar 的高度并将其用作 tab3 中的填充或边距。

使用垂直方向的线性布局..它会起作用:)