2015-02-07 42 views
0

新手到android,將按鈕對齊到屏幕中心。如何自動對齊按鈕以適應屏幕中心

這裏的代碼

<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:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context="com.kissmat.gbrf_ebook.SingupIntro" 
    android:background="@drawable/lib_gradient"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="onClick" 
     android:text="Author" 
     android:textColor="#ffffffff" 
     android:background="#00ffffff" 
     android:layout_marginLeft="70dp" 
     android:layout_marginStart="70dp" 
     android:layout_marginTop="82dp" 
     android:id="@+id/authorBtn" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:elegantTextHeight="true" 
     android:drawableTop="@drawable/author" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="onClick" 
     android:drawableTop="@drawable/publisher" 
     android:text="PUBLISHER" 
     android:textColor="#ffffffff" 
     android:background="#00ffffff" 
     android:id="@+id/pubBtn" 
     android:layout_alignTop="@+id/authorBtn" 
     android:layout_toRightOf="@+id/authorBtn" 
     android:layout_toEndOf="@+id/authorBtn" 
     android:layout_marginLeft="48dp" 
     android:layout_marginStart="58dp" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="onClick" 
     android:drawableTop="@drawable/student" 
     android:text="STUDENT" 
     android:textColor="#ffffffff" 
     android:background="#00ffffff" 
     android:layout_marginTop="57dp" 
     android:id="@+id/stbBtn" 
     android:layout_below="@+id/authorBtn" 
     android:layout_alignLeft="@+id/authorBtn" 
     android:layout_alignStart="@+id/authorBtn" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="onClick" 
     android:drawableTop="@drawable/user" 
     android:text="READER" 
     android:textColor="#ffffffff" 
     android:background="#00ffffff" 
     android:id="@+id/readerBtn" 
     android:layout_alignTop="@+id/stbBtn" 
     android:layout_alignLeft="@+id/pubBtn" 
     android:layout_alignStart="@+id/pubBtn" /> 
</RelativeLayout> 

它的外觀時,我的設備上運行。

enter image description here

+1

瞭解GridView。 – SmulianJulian 2015-02-07 12:02:45

回答

1

我只是從你的第一個按鈕刪除邊距(左,啓動和頂部),並且增加了財產android:gravity="center"到您的佈局。最後一個將內容定位在中心。沒有必要使用固定的保證金值來做到這一點。這是非常有用的,因爲您的佈局將以所有不同的屏幕尺寸和像素密度爲中心。

<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:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context="com.kissmat.gbrf_ebook.SingupIntro" 
    android:background="@drawable/lib_gradient" 
    android:gravity="center"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="onClick" 
     android:text="Author" 
     android:textColor="#ffffffff" 
     android:background="#00ffffff" 
     android:id="@+id/authorBtn" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:elegantTextHeight="true" 
     android:drawableTop="@drawable/author" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="onClick" 
     android:drawableTop="@drawable/publisher" 
     android:text="PUBLISHER" 
     android:textColor="#ffffffff" 
     android:background="#00ffffff" 
     android:id="@+id/pubBtn" 
     android:layout_alignTop="@+id/authorBtn" 
     android:layout_toRightOf="@+id/authorBtn" 
     android:layout_toEndOf="@+id/authorBtn" 
     android:layout_marginLeft="48dp" 
     android:layout_marginStart="58dp" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="onClick" 
     android:drawableTop="@drawable/student" 
     android:text="STUDENT" 
     android:textColor="#ffffffff" 
     android:background="#00ffffff" 
     android:layout_marginTop="57dp" 
     android:id="@+id/stbBtn" 
     android:layout_below="@+id/authorBtn" 
     android:layout_alignLeft="@+id/authorBtn" 
     android:layout_alignStart="@+id/authorBtn" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="onClick" 
     android:drawableTop="@drawable/user" 
     android:text="READER" 
     android:textColor="#ffffffff" 
     android:background="#00ffffff" 
     android:id="@+id/readerBtn" 
     android:layout_alignTop="@+id/stbBtn" 
     android:layout_alignLeft="@+id/pubBtn" 
     android:layout_alignStart="@+id/pubBtn" /> 
</RelativeLayout>