在Android中,在左侧和右侧放置具有等边距的按钮

时间:2016-07-05 04:20:57

标签: android android-layout button

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

enter image description here

我在Google和Stackoverflow上搜索了很多。我试图设置 android:layout_weight =" 1" 。但它只能从左侧设定相同的余量。我想在两侧和多个屏幕布局上设置它。我想知道应该为此应用哪些布局和属性。我在Android工作室使用并且大多使用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>

5 个答案:

答案 0 :(得分:5)

布局权重适用于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>

结果

enter image description here

答案 1 :(得分:4)

这是设置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>

答案 2 :(得分:4)

尝试使用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>

答案 3 :(得分: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>

它看起来像这样。

enter image description here

答案 4 :(得分:4)

       <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"/>

相关问题