中心对齐表+按钮

时间:2011-06-17 12:52:26

标签: android android-tablelayout

我希望这适用于所有屏幕尺寸,所以我不认为使用填充是解决此问题的最佳方法。当它处于垂直状态时我可以将所有内容居中,但是当它处于水平状态时,似乎没有任何东西可以对齐......

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation = "horizontal" android:layout_gravity = "center"> <TableLayout android:layout_height = "wrap_content" android:layout_width="wrap_content" android:gravity = "center" android:layout_gravity="center_horizontal"> <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:textSize="24.5sp" android:id="@+id/textView1" android:layout_height="wrap_content" android:gravity="center" android:layout_marginBottom="20dip" android:text="Bubb" android:layout_width="wrap_content"> </TextView> </TableRow> <TableRow android:baselineAligned="false"> <Button android:id = "@+id/continue_button" android:background="@drawable/and_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft = "@dimen/pad_horizontal" android:paddingRight = "@dimen/pad_horizontal" android:paddingTop = "@dimen/pad_vertical" android:paddingBottom = "@dimen/pad_vertical" android:text = "@string/first_label" android:layout_margin="@dimen/margin"/> </TableRow> <TableRow> <Button android:id = "@+id/new_button" android:text = "@string/second_label" android:background="@drawable/and_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft = "@dimen/pad_horizontal" android:paddingRight = "@dimen/pad_horizontal" android:paddingTop = "@dimen/pad_vertical" android:paddingBottom = "@dimen/pad_vertical" android:layout_margin="@dimen/margin"/> </TableRow> <TableRow> <Button android:id = "@+id/about_button" android:text = "@string/third_label" android:background="@drawable/and_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft = "@dimen/pad_horizontal" android:paddingRight = "@dimen/pad_horizontal" android:paddingTop = "@dimen/pad_vertical" android:paddingBottom = "@dimen/pad_vertical" android:layout_margin="@dimen/margin"/> </TableRow> <TableRow> <Button android:id = "@+id/exit_button" android:text = "@string/fourth_label" android:background="@drawable/and_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft = "@dimen/pad_horizontal" android:paddingRight = "@dimen/pad_horizontal" android:paddingTop = "@dimen/pad_vertical" android:paddingBottom = "@dimen/pad_vertical" android:layout_margin="@dimen/margin"/> </TableRow> </TableLayout> </LinearLayout>

任何帮助都很棒。

2 个答案:

答案 0 :(得分:1)

从您的问题中不清楚您的布局看起来如何,但我猜(至少从你的XML中)你是在追求这样的事情:

enter image description here

我没有你的drawable,所以不能包括那些。

如果是这种情况,那么这种布局可以解决问题:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:gravity="center">
    <TextView android:text="Bubb" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button android:text="Button" android:id="@+id/button1"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    <Button android:text="Button" android:id="@+id/button2"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    <Button android:text="Button" android:id="@+id/button3"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    <Button android:text="Button" android:id="@+id/button4"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>

编辑:当切换到横向时,它在垂直列表中对齐:

enter image description here

如果您需要水平列表,只需将此布局复制到“res / layout-land”并将外部LinearLayout中的方向更改为“水平”:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:gravity="center">

你会得到这个:

enter image description here

答案 1 :(得分:0)

在RelativeLayout中取TableLayout并将TableLayout的属性设置为“parent = true中的布局中心”,或者根据您的要求使用“Layout center horizo​​ntal = true”。

相关问题