Как мне добиться следующего результата, используя RelativeLayout? - PullRequest
7 голосов
/ 24 августа 2010

Картинки: http://img838.imageshack.us/img838/1402/picse.png

Как сделать макет в Рис.2, используя только RelativeLayout.Ниже приведен XML-макет первого снимка экрана.

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

18 голосов
/ 24 августа 2010

Вы можете использовать LinearLayout внизу, чтобы достичь этого. Но вы хотите использовать RelativeLayout макет только так:

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

Разве вы не можете использовать вложенные макеты?

0 голосов
/ 24 августа 2010

Если вы измените кнопки на

<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" />

вы используете по крайней мере полную ширину, хотя false_btn получает все оставшееся пространство.

Чтобы увеличить ширину первого true_btn, вы можете установить атрибут minWidth - либо использовать фиксированное значение, либо получить ширину экрана и сделать minWidth половину его

...