Android - две кнопки в одной строке, заполняющие всю ширину

У меня небольшая проблема с определением относительного макета. У меня есть представление списка с прокруткой и двумя кнопками, всегда видимыми в нижней части представления списка. Я просто хотел бы, чтобы мои две кнопки имели 50% ширины, заполняя линию. Это мой код:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:orientation="vertical" >

    <Button 
        android:id="@+id/testbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Save" />

    <Button
        android:id="@+id/cancelButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" 
        android:layout_toRightOf="@+id/testbutton"
        android:text="Cancel"/>

    <ListView android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:id="@+id/LstPeriodOptions"
        android:layout_alignParentTop="true" 
        android:layout_above="@id/testbutton" />

</RelativeLayout>

Я попытался ввести кнопки в линейный макет и присвоить гравитацию = 1 с шириной = 0 dp, но в этом случае ListView исчезает. Не могли бы вы мне помочь, пожалуйста?

Извините за мой английский. Вот результат, который я хотел бы получить:

Результат

Большое спасибо, с уважением.

РЕДАКТИРОВАТЬ: это то, что я пробовал с Linear Layout:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:orientation="vertical" >

    <LinearLayout 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/container" >

        <Button 
            android:id="@+id/testbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="Guardar" />

        <Button
            android:id="@+id/cancelButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true" 
            android:layout_toRightOf="@+id/testbutton"
            android:text="Cancelar"/>
    </LinearLayout>

    <ListView android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:id="@+id/LstPeriodOptions"
        android:layout_alignParentTop="true" 
        android:layout_above="@id/container" />

</RelativeLayout>

person puro_nervio    schedule 02.10.2013    source источник
comment
Как вы получили LinearLayout? Это должно было сработать.   -  person codeMagic    schedule 02.10.2013
comment
если вы не хотите использовать очевидный linearlayout, трюк для этого состоит в том, чтобы поместить вид 0-ширины с centerHorizontal и выровнять кнопки на нем   -  person njzk2    schedule 02.10.2013
comment
Я редактирую вопрос, показывая, как я пытался, но он не работает. Спасибо!   -  person puro_nervio    schedule 02.10.2013


Ответы (3)


Вы пробовали со своим LinearLayout таким образом, потому что это должно работать. Обратите внимание на все изменения свойств. Поскольку я не знаю, как у вас, я не могу указать все различия.

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
   <LinearLayout
      android:id="@+id/btnLL"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true">
    <Button 
        android:id="@+id/testbutton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Save" />

    <Button
        android:id="@+id/cancelButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Cancel"/>
  </LinearLayout>
    <ListView android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:id="@+id/LstPeriodOptions"
        android:layout_above="@id/btnLL" />

</RelativeLayout>
person codeMagic    schedule 02.10.2013

Попробуйте, как показано ниже, установить кнопку в LinearLayout и установить ее под ListView:

      <LinearLayout
        android:id="@+id/laytbtns"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/LstPeriodOptions" >
        <Button
            android:id="@+id/testbutton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:text="Save"/>
        <Button
             android:id="@+id/cancelButton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:text="Cancel" />
    </LinearLayout>
person GrIsHu    schedule 02.10.2013
comment
Попробуйте указать Linearlayout android:weightSum="1" и задать вес обеих кнопок 0.5 - person GrIsHu; 02.10.2013

Попробуй это..

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:orientation="vertical" >

<ListView 
        android:layout_width="match_parent"
        android:layout_height="0dp" 
            android:layout_weight="1"
        android:id="@+id/LstPeriodOptions"
        android:layout_above="@id/testbutton" />

    <LinearLayout
        android:id="@+id/laytbtns"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/LstPeriodOptions" >
        <Button
            android:id="@+id/testbutton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:text="Save"/>
        <Button
             android:id="@+id/cancelButton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:text="Cancel" />
    </LinearLayout>

</LinearLayout>
person Hariharan    schedule 02.10.2013