如何在 RelativeLayout 中将一个视图放置在另一个视图之上?

How to position a view on top of another view in RelativeLayout?

我在RelativeLayout中有一个TextView(下面用绿色表示)和一个LinearLayout(下面用红色表示)。我想将 TextView 定位在 LinearLayout 之上,如下所示:

不过,我试过这个:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/linear_layout">
    <!--some other views-->
</LinearLayout>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true" <!--this because the textview is the topmost view on the screen so I tried to use this-->
    android:layout_alignLeft="@id/linear_layout"
    android:text="my text"
    android:textSize="10pt"
    android:id="@+id/text1"/>

然而,当我运行应用程序时,它是这样的:

所以我想知道我做错了什么以及如何解决它。有我可以使用的 xml 属性吗?

如果您需要更多代码来确定问题,请随时告诉我!

像这样把你的 LinearLayout 放在 TextView 之后:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true" <!-- this because the textview is the topmost view on the screen so I tried to use this -->
    android:text="my text"
    android:textSize="10pt"
    android:id="@+id/text1" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/text1"
    android:orientation="vertical"
    android:id="@+id/linear_layout">
    <!-- some other views -->
</LinearLayout>

在Java中,您可以通过 text1.bringToFront();

在您的 LinearLayout 中添加此行:

<LinearLayout
...
android:layout_below:"@+id/text1">

</LinearLayout>

将您的布局替换为以下布局应该可行。

<TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true" 
    android:layout_alignLeft="@id/linear_layout"
    android:text="my text"
    android:textSize="10pt"
    android:id="@+id/text1"/>
<LinearLayout
    android:id="@+id/linearlayout"
    android:layout_below="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/linear_layout">
    <!--some other views-->
</LinearLayout>