繁体   English   中英

如何在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属性吗?

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

TextView一样将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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM