【发布时间】:2014-11-11 15:00:25
【问题描述】:
我正在尝试使用 Android Studio 中的设计选项卡为我的主屏幕设计我的 XML,但是当我尝试拖动一个按钮时,它会重新对齐布局上的所有内容,如下图所示:
我想安排它,使按钮大小一致,并在 2x3 表格中对齐,如下所示
a b
c d
e f
我希望图像保持在当前位置,下面的文本也不要移动。
当前 XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:orientation="vertical"
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"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/amityapplogo"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/welcomeadmin"
android:id="@+id/textView"
android:layout_gravity="center_horizontal"
android:layout_below="@+id/imageView"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mydetails"
android:id="@+id/mydetailsbutton"
android:layout_gravity="center_horizontal"
android:clickable="false"
android:layout_toStartOf="@+id/myhealthbutton"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/policydetails"
android:id="@+id/policydetailsbutton"
android:layout_gravity="center_horizontal"
android:layout_alignTop="@+id/mydetailsbutton"
android:layout_toRightOf="@+id/findaproviderbutton"
android:layout_toEndOf="@+id/findaproviderbutton" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/myhealth"
android:id="@+id/myhealthbutton"
android:layout_gravity="center_horizontal"
android:layout_toStartOf="@+id/contactusbutton"
android:layout_centerVertical="true"
android:layout_alignLeft="@+id/textView"
android:layout_alignStart="@+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/findaprovider"
android:id="@+id/findaproviderbutton"
android:layout_gravity="center_horizontal"
android:layout_toStartOf="@+id/myhealthbutton"
android:layout_below="@+id/visitourwebsitebutton"
android:layout_alignLeft="@+id/imageView"
android:layout_alignStart="@+id/imageView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/contactus"
android:id="@+id/contactusbutton"
android:layout_gravity="center_horizontal"
android:layout_toEndOf="@+id/textView"
android:layout_below="@+id/visitourwebsitebutton"
android:layout_alignRight="@+id/imageView"
android:layout_alignEnd="@+id/imageView"
android:layout_marginTop="40dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/visitourwebsite"
android:id="@+id/visitourwebsitebutton"
android:layout_gravity="center_horizontal"
android:layout_toEndOf="@+id/myhealthbutton"
android:layout_alignBaseline="@+id/myhealthbutton"
android:layout_alignBottom="@+id/myhealthbutton"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
提前干杯!
添加了 tablerows,现在所有都对齐到左侧:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:orientation="vertical"
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"
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="fill_horizontal"
android:id="@+id/">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/amityapplogo" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/welcomeadmin"
android:id="@+id/textView"
android:layout_gravity="center_horizontal" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_column="1"
android:orientation="horizontal"
android:id="@+id/">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mydetails"
android:id="@+id/mydetailsbutton"
android:layout_column="0" />
</TableRow>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/policydetails"
android:id="@+id/policydetailsbutton" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/myhealth"
android:id="@+id/myhealthbutton" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/findaprovider"
android:id="@+id/findaproviderbutton" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/visitourwebsite"
android:id="@+id/visitourwebsitebutton" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/contactus"
android:id="@+id/contactusbutton"
android:layout_gravity="center_vertical" />
</TableRow>
</TableLayout>
【问题讨论】:
-
制作表格布局,其中表格行包含按钮。
-
您需要表格格式,还是只需要按这种方式排列的按钮(没有表格轮廓)??
-
不管怎样,只要它能在不同的设备上扩展@AkshitRewari
-
GridView 或 GridLayout 可能会有所帮助。
-
@Funkystein 好喊,我用它然后变形为 tablerows。将很快添加编辑。
标签: android xml user-interface