2016-07-05 113 views
2

我是Android新手。我希望把4个按钮水平地等于边缘与左,右侧所示在下面的线框图:在左右两侧放置等边距的按钮Android

enter image description here

我搜索很多谷歌和#2也。我试图设置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> 
+0

看看的'LinearLayout' –

+2

使用'LinearLayout' layout_weight'的'概念代替的RelativeLayout的' ','layout_weight'可以和'LinearL一起使用ayout' –

+1

必须要学习:[Android布局技巧](http://android-developers.blogspot.in/2009/02/android-layout-tricks-1.html) –

回答

4

布局重量可与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

4

这是正确的设置机器人: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> 
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> 
+0

按钮之间没有空间。他想要相同的空间。 –

+0

看到我更新的答案@ Pratik Butani –

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

+0

它会使按钮之间的间隔为10 10 dp。 –

+0

是的,但您可以根据您的要求进行更改。 –

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

相关问题