2014-06-06 76 views
0
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.example.colorchangeapp.MainActivity$PlaceholderFragment" > 

<Button 
    android:id="@+id/RedPositive" 
    style="?android:attr/buttonStyleSmall" 
    android:layout_width="60dp" 
    android:layout_height="40dp" 
    android:layout_marginLeft="15dp" 
    android:layout_marginTop="85dp" 
    android:text="@string/positive_change" 
    android:onClick="redPositive"/> 

<Button 
    android:id="@+id/BluePositive" 
    style="?android:attr/buttonStyleSmall" 
    android:layout_width="60dp" 
    android:layout_height="40dp" 
    android:layout_alignTop="@+id/RedPositive" 
    android:layout_centerHorizontal="true" 
    android:text="@string/positive_change" /> 

<Button 
    android:id="@+id/GreenPositive" 
    android:layout_width="60dp" 
    android:layout_height="40dp" 
    android:layout_alignParentRight="true" 
    android:layout_alignTop="@+id/BluePositive" 
    android:layout_marginRight="16dp" 
    android:text="@string/positive_change" /> 

均勻間距我的按鈕

你怎麼能確保所創建的每個按鈕均勻地相互隔開。他們排在最前面,但我如何確保他們之間的差距完全相同?

+0

你GreenPositive marginRight是16DP,RedPositive marginLeft是15dp。除此之外,你在視覺上看到的是不正確的?看起來它應該對我有用...... – Uxonith

+0

另外,您的GreenPositive沒有其他兩個'style =「?android:attr/buttonStyleSmall」'。 – Uxonith

+0

非常感謝 – user2743857

回答

0

他們的方式我會這樣做是添加一個LinearLayout並將按鈕添加到每個權重爲1的佈局,以便它們均勻地填充線性佈局和空間。

例如

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" > 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Button" /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Button" /> 

     <Button 
      android:id="@+id/button3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Button" /> 

    </LinearLayout> 

</RelativeLayout>