2012-09-18 26 views
1

這裏的XML代碼:兩個Relativelayouts縱向顯示

<?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> 

它不是一個真正的現場(用戶界面的設計是不是代碼,但我提出它爲了簡化)。 除了這段代碼之外,TextViews會相對於另一個顯示(以垂直方式)。像:

     Welcome 
        Welcome 

但不是輸出如下:

     Welcome 

一樣,他們是嵌套在一起,彼此堆疊。 我做了寬度佈局作爲填充父母,所以爲什麼其他佈局不下來?它會覆蓋第一個相關佈局。

我必須有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"告訴它在視圖頂部佈置。

然後通過使用android:layout_below="@id/top_rel"將較低的RelativeLayout佈置在最下面的一個之下。

+1

優秀的,它的工作原理感謝的人! – idish

+0

由於太早,不能接受你的回答,我會在10分鐘內接受它。 – idish

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> 
+0

你的歡迎的白癡 –

相關問題