2013-10-02 2 views
7

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

<?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 пропадает. Не могли бы вы мне помочь, пожалуйста?

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

Result

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

EDIT: Это то, что я пытался с 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> 
+0

Как вы имеете 'LinearLayout'? Это должно сработать. – codeMagic

+0

, если вы не хотите использовать очевидный linearlayout, трюк для этого состоит в том, чтобы поместить вид ширины 0 с центром по горизонтали и выровнять на нем кнопки – njzk2

+0

. Я редактирую вопрос, как я пытался, но он не работает , Благодаря! –

ответ

29

Вы попытались с вашим 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> 
+1

Это работает как шарм. Большое спасибо! –

4

попробовать, как показано ниже, чтобы установить кнопку в 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> 
+0

Это не работает для меня. Спасибо –

+1

Попробуйте дать Linringayout 'android: weightSum =" 1 "' и дать как вес кнопки '0,5' – GrIsHu

2

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

<?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> 
Смежные вопросы