1

这是 XML 代码:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent" android:layout_width="fill_parent">
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    <TextView android:layout_height="wrap_content"
        android:layout_width="wrap_content" android:text="Welcome"
        android:layout_centerInParent="true" android:textSize="20sp"/>
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    <TextView android:layout_height="wrap_content"
        android:layout_width="wrap_content" android:text="Welcome"
        android:layout_centerInParent="true" android:textSize="20sp"/>
    </RelativeLayout>
</RelativeLayout>

这不是真正的场景(UI 设计不是那个代码,但我将其呈现为简化)。我从这段代码中得到的除了是 TextViews 将相对地(以垂直方式)一个一个地显示。喜欢:

                     Welcome
                     Welcome

但相反,输出如下:

                     Welcome

就像,它们嵌套在一起,彼此堆叠。我将宽度布局设置为填充父级,那么为什么其他布局没有下降?它覆盖了第一个相对布局。

我必须有 2 个布局,所以请不要建议我只创建一个布局。

谢谢你。

4

2 回答 2

2

您需要设置布局的关系。

试试我在下面所做的修改。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent" android:layout_width="fill_parent">
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:id="@+id/top_rel>
    <TextView android:layout_height="wrap_content"
        android:layout_width="wrap_content" android:text="Welcome"
        android:layout_centerInParent="true" android:textSize="20sp"/>
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:layout_below="@id/top_rel>
    <TextView android:layout_height="wrap_content"
        android:layout_width="wrap_content" android:text="Welcome"
        android:layout_centerInParent="true" android:textSize="20sp"/>
    </RelativeLayout>
</RelativeLayout>

我已经给出了顶部布局,android:id="@+id/top_rel"以使其更容易识别。

我还告诉它使用android:layout_alignParentTop="true".

RelativeLayout然后通过使用将较低的布局放置在顶部的下方android:layout_below="@id/top_rel"

于 2012-09-18T15:41:54.463 回答
1

首先,给第一个文本视图对应的相对布局的一些 id。然后使用 id,为第二个相对布局添加 layout_below 属性。这样它将显示在第一个文本视图的下方..

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


 <RelativeLayout
        android:id = "@+id/first_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    <TextView android:layout_height="wrap_content"
        android:layout_width="wrap_content" android:text="Welcome"
        android:layout_centerInParent="true" android:textSize="20sp"/>
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/first_layout">
    <TextView android:layout_height="wrap_content"
        android:layout_width="wrap_content" android:text="Welcome"
        android:layout_centerInParent="true" android:textSize="20sp"/>
    </RelativeLayout>
</RelativeLayout>
于 2012-09-18T15:44:51.327 回答