2016-07-05 115 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"/> 

相關問題