2011-12-13 56 views
9

我想要一個這樣的佈局: Desired layoutAndroid RelativeLayout - 如何定位?

但我的代碼不起作用。我無法實現這個佈局,我不知道我迄今爲止做了什麼錯誤。

以下是我到目前爲止 - layout_gravity是否可以嗎?還是需要將它設置在RelativeLayout

<?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="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

     <TextView 
      android:id="@+id/topText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="20dip" 
      android:layout_gravity="top" /> 

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/centerLayout" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="horizontal" 
      android:layout_below="@id/topText">  

      <ImageButton 
       android:id="@+id/lektionBackButton" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 

       android:src="@drawable/back" 
       android:layout_gravity="left"/> 
      <ImageView 
       android:id="@+id/centerImage" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:cropToPadding="true" 
       android:layout_gravity="center"/> 
      <ImageButton 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/lektionForwardButton" 
       android:src="@drawable/forward" 
       android:layout_gravity="right"/>   
     </LinearLayout>  

     <TextView 
      android:id="@+id/bottomText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="20dip" 
      android:layout_below="@id/centerLayout" 
      android:layout_gravity="bottom" /> 
    </RelativeLayout> 
</RelativeLayout> 
+2

很好的問題,完美的捕捉。 – 2011-12-14 07:03:43

回答

15

根本不需要使用重力或內部LinearLayout。請使用layout_alignParentToplayout_alignParentLeft,layout_alignParentRight,layout_alignParentBottom,layout_centerInParentlayout_centerVertical上的孩子。

您可能需要仔細閱讀RelativeLayout tutorial

2

只有涉及相對佈局時,您可以切換到Eclipse編輯器中的圖形佈局,然後拖放並設置屬性。一切只需用鼠標。然而,這對任何其他佈局都是不可能的。希望有所幫助。