【问题标题】:Equal space between buttons in a LinearLayout线性布局中按钮之间的相等间距
【发布时间】:2020-02-17 19:37:54
【问题描述】:

我在 LinearLayout 中有四个垂直排列的按钮。

我希望按钮之间的空间以及最顶部按钮和线性布局顶部之间的空间以及最底部按钮和布局底部之间的空间相同。

在附图中,空格被描绘为红色路径。 我希望所有空间都具有相同的大小。

我将如何实现我的目标?

<LinearLayout
            p1:orientation="vertical"
            p1:layout_width="wrap_content"
            p1:layout_height="wrap_content"
            p1:id="@+id/mainButtonLayout">
            <Button
                p1:text="xxx"
                p1:layout_width="match_parent"
                p1:layout_height="wrap_content"
                p1:id="@+id/saButton"
                p1:textColor="#FFFFFF"
                p1:background="@drawable/roundedBlue"
                p1:minHeight="33dp"
                p1:minWidth="175dp"
                p1:layout_marginBottom="20dp" />
            <Button
                p1:text="xxxx"
                p1:layout_width="match_parent"
                p1:layout_height="wrap_content"
                p1:id="@+id/rButton"
                p1:textColor="#FFFFFF"
                p1:background="@drawable/roundedBlue"
                p1:minHeight="33dp"
                p1:minWidth="175dp"
                p1:layout_marginBottom="20dp" />
            <Button
                p1:text="xxxxx"
                p1:layout_width="match_parent"
                p1:layout_height="wrap_content"
                p1:id="@+id/sButton"
                p1:textColor="#FFFFFF"
                p1:background="@drawable/roundedBlue"
                p1:minHeight="33dp"
                p1:minWidth="175dp"
                p1:layout_marginBottom="20dp" />
            <Button
                p1:text="xxxxx"
                p1:layout_width="match_parent"
                p1:layout_height="wrap_content"
                p1:id="@+id/pButton"
                p1:textColor="#FFFFFF"
                p1:background="@drawable/roundedBlue"
                p1:minHeight="33dp"
                p1:minWidth="175dp" />
        </LinearLayout>

【问题讨论】:

    标签: android android-layout button android-linearlayout


    【解决方案1】:

    在您的 LinearLayout 中使用 weightsum

    <LinearLayout
    ...
    android:weightSum="4.0">`
    

    在每个 Button 中放置 layout_height = 0dp , layout_weight = 1, 上下边距根据您的需要而定,但每个按钮都相同。

    <Button....
    ...
    android:layout_height="0dp"
    android:layout_weight="1.0"
    android:layout_marginBottom="40dp"
    android:layout_marginTop="40dp"
    />
    

    更多信息:read this document

    【讨论】:

      【解决方案2】:

      将按钮放置在布局的中心:

      <LinearLayout
          android:orientation="vertical"
          android:gravity="center_vertical"
          ... >
      

      要在按钮之间放置空格,我可以想到两个选项。第一种是在除第一个按钮之外的所有按钮上使用android:layout_marginTop。第二种是用硬编码的大小在 XML 中绘制一个空的形状,并将其用作 LinearLayout 的分隔符

      <shape xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="rectangle" >
      
          <size android:width="your_dimension_here"
              android:height="your_dimension_here" />
      
          <solid android:color="@android:color/transparent" />
      </shape
      
      <LinearLayout
          android:divider="@drawable/divider_space"
          android:showDividers="middle"
          ... >
      

      【讨论】:

      • 您的解决方案是否独立于分辨率?
      • @Pétur 如果您使用以 dp 为单位指定的尺寸,则无论您喜欢哪种方法来设置按钮之间的间距。
      【解决方案3】:

      希望我会尽力完成您的要求。试试这个xml

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          android:gravity="center" >
         <Button
              android:id="@+id/btn1"
              android:layout_width="50dp"
              android:layout_height="50dp"
               />
      
          <View
              android:layout_width="0dp"
              android:layout_height="50dp"
              android:layout_weight="1" />
      
          <Button
              android:id="@+id/btn2"
              android:layout_width="50dp"
              android:layout_height="50dp"
             />
      
          <View
              android:layout_width="0dp"
              android:layout_height="50dp"
              android:layout_weight="1" />
      
          <Button
              android:id="@+id/btnTwitter"
              android:layout_width="50dp"
              android:layout_height="50dp"
              />
      
          <View
              android:layout_width="0dp"
              android:layout_height="50dp"
              android:layout_weight="1" />
      
          <Button
              android:id="@+id/btn3"
              android:layout_width="50dp"
              android:layout_height="50dp"
               />
      </LinearLayout>
      

      【讨论】:

        【解决方案4】:

        在你的视图之间使用这个,

        <View
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_weight="1" />
        

        【讨论】:

          【解决方案5】:

          只需将 p1:gravity="center" 添加到您的线性布局中。

          <LinearLayout
          p1:layout_width="match_parent"
          p1:layout_height="match_parent"
          p1:orientation="vertical"
          p1:gravity="center"
          p1:id="@+id/mainButtonLayout">
          

          【讨论】:

          • 这不起作用,它将按钮向左和向右拉伸,但垂直空间保持不变。