2010-08-24 37 views
7

Ảnh: http://img838.imageshack.us/img838/1402/picse.pngLàm cách nào để đạt được kết quả sau bằng cách sử dụng RelativeLayout?

Làm cách nào để bố cục trong Pic. 2 chỉ sử dụng RelativeLayout ONLY. Dưới đây là bố cục xml của ảnh chụp màn hình đầu tiên.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
<TextView 
    android:id="@+id/timer_textview" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:layout_marginTop="10dip" 
    android:textSize="30sp" 
    android:layout_alignParentTop="true" 
    android:text="@string/timer_start" /> 

<ListView 
    android:id="@+id/questions_listview" 
    android:layout_below="@id/timer_textview" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" /> 

<Button android:id="@+id/true_btn" 
    android:text="@string/true_option" 
    android:layout_alignParentBottom="true" 
    android:layout_weight="1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

<Button 
    android:id="@+id/false_btn" 
    android:text="@string/false_option" 
    android:layout_toRightOf="@id/true_btn" 
    android:layout_alignBaseline="@id/true_btn" 
    android:layout_weight="1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
</RelativeLayout> 
+2

'android: layout_weight' không có nghĩa trong' RelativeLayout'. Tôi không biết cách để thực hiện những gì bạn tìm kiếm bằng cách sử dụng một 'RelativeLayout' duy nhất, xin lỗi. – CommonsWare

Trả lời

18

Bạn có thể sử dụng LinearLayout ở phía dưới để đạt được điều này . Nhưng bạn chỉ muốn sử dụng bố cục RelativeLayout như vậy:

<RelativeLayout android:layout_width="fill_parent" ... > 

...  

<View android:id="@+id/helper" 
android:layout_width="0dp" 
android:layout_height="0dp" 
android:layout_alignParentBottom="true" 
android:layout_centerHorizontal="true" /> 

<Button android:id="@+id/true_btn" 
android:layout_alignParentBottom="true" 
android:layout_alignParentLeft="true" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_toLeftOf="@id/helper"/> 

<Button 
android:id="@+id/false_btn" 
android:layout_alignBaseline="@id/true_btn" 
android:layout_alignParentRight="true" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_toRightOf="@id/helper" /> 

</RelativeLayout> 

Bạn không thực sự sử dụng bố cục lồng nhau?

+0

Tôi biết tôi có thể sử dụng bố cục lồng nhau, trong ảnh chụp màn hình thứ hai tôi đã thực hiện chính xác điều đó. Nhưng tôi chỉ muốn biết những hạn chế của RelativeLayout. –

+0

Bạn đã thử mã của tôi chưa? – plugmind

+0

Rất tiếc ... Không hoạt động. Dù sao, hãy cảm ơn @ radek-k –

0

Nếu bạn thay đổi các nút để

<Button android:id="@+id/true_btn" 
    android:text="@string/true_option" 
    android:layout_alignParentBottom="true" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

<Button 
    android:id="@+id/false_btn" 
    android:text="@string/false_option" 
    android:layout_toRightOf="@id/true_btn" 
    android:layout_alignBaseline="@id/true_btn" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" /> 

bạn sử dụng ít nhất là đầy đủ chiều rộng mặc dù false_btn được tất cả không gian còn lại.

Để tăng độ rộng của true_btn đầu tiên bạn có thể thiết lập các thuộc tính minWidth - hoặc sử dụng một giá trị cố định hoặc có được chiều rộng màn hình và làm cho minWidth một nửa của nó

+0

Hãy để tôi thử và tôi sẽ liên hệ lại với bạn về vấn đề này. –

+0

ý tưởng của bạn là tốt, tôi chỉ muốn biết là có những cách để xác định chiều rộng của màn hình chung? (ví dụ) Nên hoạt động trên tất cả các thiết bị có kích thước màn hình khác nhau. –

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