Android - 同一行中的两个按钮填充整个宽度

时间:2013-10-02 12:59:54

标签: android layout relativelayout

我在定义相对布局时遇到了一些问题。我有一个带有滚动的列表视图,并且在列表视图的底部始终可以看到两个按钮。我只是希望我的两个按钮有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>

我尝试在线性布局中引入按钮,并使用width = 0dp给出gravity = 1,但在这种情况下,ListView消失了。你能帮帮我吗?

抱歉我的英文。这是我想要的结果:

Result

非常感谢,最诚挚的问候。

编辑:这是我尝试使用线性布局:

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

3 个答案:

答案 0 :(得分:35)

您是否以这种方式尝试使用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>

答案 2 :(得分: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>
相关问题