【问题标题】:Elements superposed using Android RelativeLayout使用 Android RelativeLayout 叠加的元素
【发布时间】:2026-02-10 10:00:02
【问题描述】:

我有以下 Android 布局

<?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:paddingBottom="16dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/slideTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dip"
        android:padding="@dimen/px20"
        android:text="@string/login_message"
        android:textSize="@dimen/px25"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/slideDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/password"
        android:drawablePadding="@dimen/px20"
        android:drawableStart="@drawable/password"
        android:padding="@dimen/px20"
        android:text="@string/login_message_body"
        android:textSize="@dimen/px20" />

    <TextView
        android:id="@+id/swipeMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="" />

</RelativeLayout>

但是所有元素都位于屏幕顶部,一个在另一个之上,就像它们不占用任何空间一样。

RelativeLayout documentation 中似乎不是这样,所有元素都垂直放置在一个下方。

这是怎么回事?

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    所以你需要使用其他组件的 id 来正确对齐。

    例如,ID 为 @+id/slideDescriptionTextView 也应该有 android:layout_below="@+id/slideTitle" 作为 xml 的属性之一。

    ID 为 @+id/swipeMessageTextView 也应该有 android:layout_below="@+id/slideDescription" 作为 xml 的属性之一。

    【讨论】:

    • 谢谢!两个答案都是正确的,我接受了这个,因为它更详细。
    【解决方案2】:

    为了在 RelativeLayout 中将一个视图置于另一个视图下方,您必须使用layout_below 属性并将您想要的视图 ID 设置为高于指定视图的 ID。但实际上为了将视图垂直放置在彼此下方,使用 LinearLayout 并将方向设置为垂直会更方便

    【讨论】:

    • 太棒了,谢谢!我有一个 LinearLayout 但我切换到 RelativeLayout 因为我想用 android:layout_alignParentBottom="true" 将一个元素放在屏幕底部。
    • 这就是RelativeLayout 的子项的相对性所提供的。
    【解决方案3】:

    上面的xml代码中缺少layout_below。我用那个替换了代码,请使用那个。

    在相对布局中,元素将相对于其他元素进行排列,为了做到这一点,我们应该使用各个视图元素的 id 值

    android:layout_below="@id/slideTitle" 应该放在描述文本视图中 android:layout_below="@id/slideDescription" 应该放在消息文本视图中

    为了得到你想要的输出,请使用下面的代码

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="16dp" >
    
    <TextView
        android:id="@+id/slideTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dip"
        android:padding="@dimen/px20"
        android:text="@string/login_message"
        android:textSize="@dimen/px25"
        android:textStyle="bold" />
    
    <TextView
        android:id="@+id/slideDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/slideTitle"
        android:drawableLeft="@drawable/password"
        android:drawablePadding="@dimen/px20"
        android:drawableStart="@drawable/password"
        android:padding="@dimen/px20"
        android:text="@string/login_message_body"
        android:textSize="@dimen/px20" />
    
    <TextView
        android:id="@+id/swipeMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@id/slideDescription"
        android:text="" />
    

    【讨论】: