2011-12-13 34 views
9

Tôi muốn có Bố cục như sau: Desired layoutAndroid RelativeLayout - cách đặt vị trí?

Nhưng mã của tôi không hoạt động. Tôi không thể đạt được Bố cục này, và tôi không biết có gì sai với những gì tôi đã làm cho đến nay.

Đây là những gì tôi có cho đến nay - Có phải layout_gravity OK không? Hay nó cần phải được đặt trong số 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

câu hỏi hay với snap hoàn hảo. –

Trả lời

15

Bạn không cần phải sử dụng lực hấp dẫn hoặc bên trong là LinearLayout. Thay vào đó, hãy sử dụng layout_alignParentTop, layout_alignParentLeft, layout_alignParentRight, layout_alignParentBottom, layout_centerInParentlayout_centerVertical đối với trẻ em.

Có thể hữu ích khi bạn đi qua số RelativeLayout tutorial.

2

Chỉ khi nói đến bố cục tương đối, bạn có thể chuyển sang bố cục đồ họa trong trình soạn thảo Eclipse của bạn và kéo và đặt thuộc tính. Tất cả mọi thứ chỉ với con chuột của bạn. Tuy nhiên, điều này là không thể với bất kỳ bố cục nào khác. Hy vọng rằng sẽ giúp.

Các vấn đề liên quan