如何自动对齐按钮以适应屏幕中心
问题描述:
新手到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>
它的外观时,我的设备上运行。
答
我只是从你的第一个按钮删除边距(左,启动和顶部),并且增加了财产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>
了解GridView。 – SmulianJulian 2015-02-07 12:02:45