【问题标题】:put space between android buttons在android按钮之间放置空间
【发布时间】:2013-08-03 04:51:48
【问题描述】:
<Button
    android:id="@+id/o_pharmacy"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/p2"
    android:text="@string/o_pharmacy"
    android:textSize="26sp" />

<Button
    android:id="@+id/lab"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/lab"
    android:text="@string/lab"
    android:textSize="26sp" />

<Button
    android:id="@+id/i_pharmacy"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/p1"
    android:text="@string/i_pharmacy"
    android:textSize="26sp" />

我尝试使用上面的代码在 Liner 布局中显示 3 个按钮。它可以工作,但我需要在两个按钮之间留出空间。

【问题讨论】:

  • LinearLayout 的方向是什么?

标签: android button android-linearlayout


【解决方案1】:
android:layout_margin="10dp"

每个按钮

【讨论】:

    【解决方案2】:

    如果您的 LinearLayout 的方向是垂直的,请使用

    android:layout_marginTop="10dp"
    

    否则,使用

    android:layout_marginLeft="10dp"
    

    【讨论】:

      【解决方案3】:
      <Button
          android:id="@+id/o_pharmacy"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:drawableLeft="@drawable/p2"
          android:text="@string/o_pharmacy"
          android:textSize="26sp" />
      
      <Button
          android:id="@+id/lab"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_margin="25dp"
          android:drawableLeft="@drawable/lab"
          android:text="@string/lab"
          android:textSize="26sp" />
      
      <Button
          android:id="@+id/i_pharmacy"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_margin="25dp"
          android:drawableLeft="@drawable/p1"
          android:text="@string/i_pharmacy"
          android:textSize="26sp" />
      

      试试这个。

      【讨论】:

        【解决方案4】:

        在水平线性布局中的 3 个按钮之间留出空间以在中心按钮上使用它的最简单方法:

        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        

        如果你有垂直的LinearLayout,你可以使用marginTop和marginBottom。

        【讨论】:

          【解决方案5】:
          <LinearLayout 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/o_pharmacy"
                          android:layout_width="fill_parent"
                          android:layout_height="wrap_content"
          
                          android:layout_marginTop="10dip"
          
                          android:drawableLeft="@drawable/p2"
                          android:text="@string/o_pharmacy"
                          android:textSize="26sp" />
          
                             <Button
                             android:id="@+id/lab"
                             android:layout_width="fill_parent"
                             android:layout_height="wrap_content"
          
                             android:layout_marginTop="10dip"
          
                             android:drawableLeft="@drawable/lab"
                             android:text="@string/lab"
                             android:textSize="26sp" />
          
                             <Button
                             android:id="@+id/i_pharmacy"
                             android:layout_width="fill_parent"
                             android:layout_height="wrap_content"
          
                             android:layout_marginTop="10dip"
          
                             android:drawableLeft="@drawable/p1"
                             android:text="@string/i_pharmacy"
                             android:textSize="26sp" />
          

          我假设您为 LinearLayout 使用 垂直方向,否则此代码将没有意义,因为您的按钮是 Fill_parent for layout_width。请注意android:layout_marginTop="10dip" 的行,它确保您在按钮之间留出合理的 10 个倾斜空间。当然,您可以增加(或减少)按钮之间的空间。那是你的选择。

          希望这能令人满意地回答您的问题。

          【讨论】:

            【解决方案6】:
             android:layout_marginBottom="50dp"
            
             android:layout_marginTop="50dp"
            

            【讨论】:

              【解决方案7】:

              最好在您的 XML 活动中使用 android:layout_marginTop="10dp",因为这样可以在该按钮和其他按钮或小部件之间提供准确的间距。 对其余按钮重复此操作。 快乐编程!

              【讨论】:

              【解决方案8】:

              我想你可以试试 layout_weight。如果您需要连续 3 个,中间有一些空间。你可以这样做

              <LinearLayout
                                android:id="@+id/buttons"
                                android:orientation="horizontal"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
              
                                >
              
                                  <com.google.android.material.button.MaterialButton
                                    android:id="@+id/o_pharmacy"
                                    android:layout_width="0dp"
                                    android:layout_height="64dp"
                                    android:layout_weight="1"
                                    android:layout_marginLeft="16dp"
                                    android:text="@string/o_pharmacy"
              
              
                                    />
              
                                  <com.google.android.material.button.MaterialButton
                                    android:id="@+id/lab"
                                    android:layout_width="0dp"
                                    android:layout_height="64dp"
                                    android:layout_weight="1"
                                    android:layout_marginLeft="16dp"
                                    android:padding="5dp"
                                    android:text="@string/lab"
              
              
                                    />
              
                                  <com.google.android.material.button.MaterialButton
                                    android:id="@+id/i_pharmacy"
                                    android:layout_width="0dp"
                                    android:layout_height="64dp"
                                    android:layout_weight="1"
                                    android:layout_marginLeft="16dp"
                                    android:layout_marginRight="16dp"
                                    android:text="@string/i_pharmacy"
              
              
                                    />
              
                              </LinearLayout>
              

              【讨论】:

                【解决方案9】:

                最好用

                 android:textSize="26dp"
                

                而不是

                android:textSize="26sp"
                

                在所有不同尺寸的设备上都有更好的结果!

                【讨论】:

                • 不。文字大小需要sp
                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-07-12
                • 2012-03-16
                • 2015-11-23
                • 2018-06-05
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多