Android 安卓-两个按钮在同一行填充整个宽度

Android 安卓-两个按钮在同一行填充整个宽度,android,layout,android-relativelayout,Android,Layout,Android Relativelayout,我在定义相对布局时有点问题。我有一个带滚动的列表视图,在列表视图的底部有两个始终可见的按钮。我只想我的两个按钮有50%的宽度,填充线。这是我的代码: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" andr

我在定义相对布局时有点问题。我有一个带滚动的列表视图,在列表视图的底部有两个始终可见的按钮。我只想我的两个按钮有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>

尝试如下操作,在
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>

试试这个

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


您是如何获得
线性布局的
?这应该是可行的。如果你不想使用明显的线性布局,诀窍是将一个0宽度的视图与中心水平放置,并对齐itI上的按钮。编辑显示我如何尝试的问题,但它不起作用。谢谢尝试给出线性布局
android:weightSum=“1”
并给出按钮的重量
0.5
<?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>