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

2024-05-02

我在定义相对布局时遇到了一些问题。我有一个带有滚动的列表视图,并且两个按钮始终在列表视图的底部可见。我只是希望我的两个按钮有 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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐