【问题标题】:Put buttons with Equal margins on both left and right side in Android在Android的左右两侧放置具有相等边距的按钮
【发布时间】:2016-07-05 20:30:45
【问题描述】:

我是 Android 新手。我想水平放置 4 个按钮,左右边距相等,如下线框图所示:

我也在 Google 和 Stackoverflow 上进行了很多搜索。我试图设置 android:layout_weight="1" 。但它只从左侧设置相等的边距。我想在两侧和多个屏幕布局上设置它。我想知道应该为此应用哪些布局和属性。我在 Android Studio 中使用,主要使用 Drag-Drop 方法进行设计。

目前我的 XML 布局如下:

  <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/frameLayout"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="10dp"
    android:id="@+id/relativeLayout">

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="3"
        android:id="@+id/b3"
        android:layout_gravity="left|center_vertical"
        android:onClick="buttonThree"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="false"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="false"
        android:layout_weight="1"
        android:width="0dp" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="5"
        android:id="@+id/b5"
        android:layout_gravity="left|center_vertical"
        android:onClick="buttonFive"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/b3"
        android:layout_toEndOf="@+id/b3"
        android:layout_alignParentRight="false"
        android:layout_alignParentLeft="false"
        android:layout_weight="1"
        android:width="0dp"
        android:layout_alignParentBottom="false" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="7"
        android:id="@+id/b7"
        android:layout_gravity="left|center_vertical"
        android:onClick="buttonSeven"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/b5"
        android:layout_toEndOf="@+id/b5"
        android:layout_alignParentRight="false"
        android:layout_weight="1"
        android:width="0dp" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="9"
        android:id="@+id/b9"
        android:layout_gravity="left|center_vertical"
        android:onClick="buttonNine"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/b7"
        android:layout_toEndOf="@+id/b7"
        android:layout_alignParentRight="false"
        android:layout_weight="1"
        android:width="0dp" />

</RelativeLayout>

【问题讨论】:

  • 看看layout_weight的概念LinearLayout
  • 使用LinearLayout 代替RelativeLayoutlayout_weight 将与LinearLayout 一起使用
  • 不要对他的问题投反对票,他正在努力学习

标签: android android-layout button


【解决方案1】:

布局权重与 LinearLayout 一起使用,linearlayout 中的每个视图都将采用 designeatedweight/weightsum 乘以总宽度或高度。

将您的按钮移动到权重总和为 4 的线性布局。将按钮的权重分配为 1 将使它们自动占据屏幕空间的 1/4。 对于等间距,将边距分配给线性布局将使它们看起来等间距。

    <Button
        android:id="@+id/b3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:onClick="buttonThree"
        android:text="3" />

    <Button
        android:id="@+id/b5"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"

        android:layout_weight="1"
        android:onClick="buttonFive"
        android:text="5" />

    <Button
        android:id="@+id/b7"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:onClick="buttonSeven"
        android:text="7" />

    <Button
        android:id="@+id/b9"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:onClick="buttonNine"
        android:text="9" />

</LinearLayout>

结果

【讨论】:

    【解决方案2】:

    设置 android:layout_weight="1" 是对的,但只有 Linearlylayout 有效,所以可以

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal" 
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <Button
    
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_marginLeft="3dp"
            android:layout_marginRight="3dp"
            android:layout_height="wrap_content" />
        <Button
    
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_marginLeft="3dp"
            android:layout_marginRight="3dp"
            android:layout_height="wrap_content" />
        <Button
    
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_marginLeft="3dp"
            android:layout_marginRight="3dp"
            android:layout_height="wrap_content" />
        <Button
    
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_marginLeft="3dp"
            android:layout_marginRight="3dp"
            android:layout_height="wrap_content" />
    
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      尝试使用LinearLayout

      <?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="wrap_content"
      android:layout_marginTop="3dp"
      android:layout_marginBottom="3dp"
      android:layout_marginLeft="3dp"
      android:layout_marginRight="3dp"
      android:orientation="horizontal">
      
      <Button 
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:layout_marginRight="3dp"
          android:layout_marginLeft="3dp"
          android:text="B1"/>
      
      <Button 
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:layout_marginRight="3dp"
          android:text="B2"/>
      
      <Button 
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:layout_marginRight="3dp"
          android:text="B3"/>
      
      <Button 
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:layout_marginRight="3dp"
          android:text="B4"/>
      
      </LinearLayout>
      

      【讨论】:

      • 按钮之间没有空格。他想要同样的空间。
      【解决方案4】:

      试试这个。

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          android:padding="5dp"
          android:weightSum="4" >
      
          <Button
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_margin="5dp"
              android:layout_weight="1"
              android:text="B1"
              android:textColor="#fff" />
      
          <Button
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_margin="5dp"
              android:layout_weight="1"
              android:text="B1"
              android:textColor="#fff" />
      
          <Button
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_margin="5dp"
              android:layout_weight="1"
              android:text="B1"
              android:textColor="#fff" />
      
          <Button
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_margin="5dp"
              android:layout_weight="1"
              android:text="B1"
              android:textColor="#fff" />
      
      
      </LinearLayout>
      

      它看起来像这样。

      【讨论】:

      • 它将在按钮之间留出 10 10 dp 的空间。
      • 可以,但您可以根据自己的需要进行更改。
      【解决方案5】:
             <LinearLayout 
                 xmlns:android="http://schemas.android.com/apk/res/android"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:orientation="horizontal"
                 android:weightSum="3"
              >
      
      <Button android:id="@+id/button1"
              ...
              android:layout_weight="1"/>
      
      <Button android:id="@+id/button2"
              ...
              android:layout_weight="1"/>
      
      <Button
          android:id="@+id/button3"
          ...
          android:layout_weight="1"/>
      

      【讨论】: