【问题标题】:Android button layout - get two buttons side-by-side across entire screenAndroid 按钮布局 - 在整个屏幕上并排获取两个按钮
【发布时间】:2026-02-21 13:15:02
【问题描述】:

我有两个按钮,我希望它们水平并排显示,但它们一起填充了手机的水平长度。高度是包装内容,这很好。我现在的问题是只显示一个按钮(在屏幕上伸展)。

这是我的 XML 代码:

<LinearLayout 
   android:id="@+id/page_buttons"
   android:orientation="horizontal"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:gravity="center_horizontal"

   >
   <Button
        android:id="@+id/prevButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Previous"
   /> 

   <Button
        android:id="@+id/nextButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Next"
   />

【问题讨论】:

  • 鉴于您在 xml 中只指定了一个按钮,您希望屏幕上出现两个按钮的具体情况如何?您是否动态创建第二个并将其添加到线性布局?
  • 我编辑了帖子以使其现在可读。

标签: android xml button android-linearlayout


【解决方案1】:

更改您的 Buttons XML 以包含 layout_weight 属性:

<Button android:id="@+id/nextButton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Next"/>

【讨论】:

  • 除了公认的答案之外,我认为值得指出的是,包裹两个按钮的线性布局应该有选项android:orientation="horizontal"。我很慢,一开始没听懂。
【解决方案2】:

由于没有人解释他们的解决方案为什么有效,我会试一试。

问题在于按钮的布局。关注的两个属性是 layout_width 和 layout_weight。

在其他布局系统中,当您指出布局的每个元素都必须填充父元素时(layout_width="fill_parent"),它们会通过在它们之间平均分配父元素的空间来做到这一点。因此,它们中的每一个都将具有相同的大小。

相反,在 Android 的布局系统中,如果两个元素都有 layout_width="fill_parent" 第一个元素(在您的情况下, 预览按钮)将被拉伸以填充父级和第二个 (或第三个,等等)将没有任何空间可以分配,所以它 将不可见。

现在,为了理解您希望两个按钮都显示,您为每个按钮设置了 layout_weight。要使按钮具有相同的大小,请为它们设置相同的布局权重。

layout_weight 定义了每个按钮占据父级的多少“部分”(或段)。父级将被切割成等于子级段总和的段数。如果你想让一个按钮比另一个大三倍,你必须为其分配的部件数等于第一个按钮的部件数乘以三。

因此,如果您希望“下一步”按钮大两倍,那么 预览按钮,你可以这样做:

  • 预览按钮:layout_weight=1
  • 下一个按钮:layout_weight=2

因此,父级将被分成 3 个部分,其中 2 个将分配给 Next 按钮,1 个分配给 Previews 按钮。

这里的例子是按钮和水平布局,但这对于任何类型的对象和垂直布局父级都适用。

【讨论】:

  • 非常感谢您的解释
【解决方案3】:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
<Button
  android:id="@+id/button01"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:layout_below="@id/entry" 
  android:layout_alignParentLeft="true"
  android:layout_toLeftOf="@+id/space"/>
<TextView
  android:id="@+id/space"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:layout_centerHorizontal="true"/>
<Button
  android:id="@+id/button02"
  android:layout_toRightOf="@id/button01"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_below="@id/entry" 
  android:layout_alignParentRight="true"/>  
</RelativeLayout>

似乎您想要两个相等的按钮,而不是包装的内容。我使用 TextView 创建了一个居中的间隔,并与之相对对齐。左键到父级左侧和间隔,右键到左键和父级。

【讨论】:

    【解决方案4】:

    您的 Button 的父级是线性布局,并且您将 Button 的宽度设置为填充父级,因此它占用了整个空间。你有两个选项来实现这个......

    1. 为您的按钮指定固定宽度(例如 50dip)。
    2. 将宽度指定为 0dp,并在两个按钮“权重”中再插入一个属性,并为每个按钮指定 0.5dip...

    【讨论】:

      【解决方案5】:

      这两个按钮应该在一个线性布局上。线性布局应该是水平的,每个按钮的权重为 1。这应该给你两个沿屏幕宽度大小相等的按钮。样品在这里 Horizontal orientation: check the 2 Buttons at the end of this xml layout

      【讨论】:

        【解决方案6】:

        您的要求是

        <LinearLayout 
           android:id="@+id/page_buttons"
           android:orientation="horizontal"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
        
           >
           <Button
                android:id="@+id/prevButton"
                android:layout_width="0dp"
                android:weight="0.5"
                android:layout_height="wrap_content"
                android:text="Previous"
           /> 
        
           <Button
                android:id="@+id/nextButton"
                android:layout_width="0dp"
                android:weight="0.5"
                android:layout_height="wrap_content"
                android:text="Next"
           />
        

        【讨论】:

          【解决方案7】:
          <?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="wrap_content" 
              android:layout_centerHorizontal="true"> 
          
              <TextView 
                  android:text="@+id/SomeText" 
                  android:id="@+id/TextView01" 
                  android:layout_width="wrap_content" android:layout_height="wrap_content" /> 
          
              <LinearLayout 
                  android:orientation="horizontal" 
                  android:background="@android:drawable/bottom_bar" 
                  android:paddingLeft="4.0dip" 
                  android:paddingTop="5.0dip" 
                  android:paddingRight="4.0dip" 
                  android:paddingBottom="1.0dip" 
                  android:layout_width="fill_parent" android:layout_height="wrap_content" 
                  android:layout_below="@+id/TextView01"> 
          
                  <Button 
                      android:id="@+id/allow" 
                      android:layout_width="0.0dip" android:layout_height="fill_parent" 
                      android:text="Allow" 
                      android:layout_weight="1.0" /> 
          
                  <Button 
                      android:id="@+id/deny" 
                      android:layout_width="0.0dip" android:layout_height="fill_parent" 
                      android:text="Deny" 
                      android:layout_weight="1.0" /> 
          
              </LinearLayout> 
          </RelativeLayout> 
          

          【讨论】:

            【解决方案8】:

            对于那些似乎仍然没有得到他们想要的东西的人来说,我发现了主要问题。 当使用具有常规(开箱即用/默认)背景的按钮时,它具有内置边距。 如果您尝试使用单一颜色手动更改背景,您可以轻松判断您只需要设置单独的背景。 除此之外,当您放入重量时,它会起作用。

            【讨论】: