【问题标题】:Android - Two buttons in same line filling the whole widthAndroid - 同一行中的两个按钮填充整个宽度
【发布时间】:2026-02-21 01:45:01
【问题描述】:

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

我尝试在线性布局中引入按钮,并在宽度=0dp 的情况下赋予重力=1,但在这种情况下,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 的?那应该行得通。
  • 如果你不想使用明显的线性布局,这样做的诀窍是放置一个 0 宽度的 view 与 centerHorizo​​ntal,并在其上对齐按钮
  • 我编辑了显示我是如何尝试的问题,但它不起作用。谢谢!

标签: android layout android-relativelayout


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    尝试如下将您的按钮设置为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>
    

    【讨论】:

    • 尝试给 Linearlayout android:weightSum="1" 和按钮的权重0.5
    【解决方案3】:

    试试这个..

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

    【讨论】: