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 с ширина=0dp, но в този случай ListView изчезва. Бихте ли ми помогнали?

Съжалявам за моя английски. Това е резултатът, който бих искал да имам:

Резултат

Благодаря много, най-добри пожелания.

РЕДАКТИРАНЕ: Това е, което опитах с линейно оформление:

<?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
ако не искате да използвате очевидното линейно оформление, трикът за това е да поставите изглед с ширина 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