Android: 如何将多个ImageButton居中对齐

5

我有两个像这样的ImageButton

enter image description here

我尝试了各种方法来使它们在屏幕中央对齐。作为一个新手,我不知道该怎么做。如何实现我的目标,在屏幕中央对齐这两个ImageButton呢?

这是我的布局xml

<TableLayout android:id="@+id/TableLayout01"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:stretchColumns="1">
        <TableRow android:id="@+id/TableRow01" android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <ImageButton android:id="@+id/btn_Show"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:text="Show" android:src="@drawable/cmd_result_show"
                android:layout_gravity="right" android:background="@android:color/transparent" />

            <ImageButton android:id="@+id/btn_showAll"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:text="Detailed Show" android:layout_gravity="center"
                android:src="@drawable/cmd_result_details" android:background="@android:color/transparent" />
        </TableRow>
    </TableLayout>

请为您使用过的代码添加注释,或者如果您遇到任何问题,请说明...同时请接受正确答案。 - Anju
3个回答

7

我希望您可以直接为这些按钮提供图像。因此,该设置的 text 属性无用。使用 RelativeLayout 可以轻松开发这种类型的屏幕。

下面是代码,请查看并添加注释,如果有任何问题。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:gravity="center_horizontal">

    <ImageButton android:id="@+id/btn_Show"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:background="@drawable/cmd_result_show" />

    <ImageButton android:id="@+id/btn_showAll"
        android:layout_toRightOf="@id/btn_Show" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:background="@drawable/cmd_result_details" />
</RelativeLayout>

谢谢,运行得非常完美。我在两个ImageButton之间添加了一个空的TextView,以便留出一些间隔。谢谢。 - Sarfarosh Lakhan
2
如果您想要添加空间,不需要为此添加TextView...添加margin属性就足够了。对于您的第一个按钮,可以使用android:layout_marginRight="30dp"之类的属性...它将添加边距,从而增加间距... - Anju

0
    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:gravity="center">
  <TableLayout android:id="@+id/TableLayout01"
    android:stretchColumns="1" android:layout_height="wrap_content"
    android:layout_width="wrap_content" >
    <TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
        <ImageButton android:id="@+id/btn_Show"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="Show" android:src="@drawable/cmd_result_show"
            android:layout_gravity="right" android:background="@android:color/transparent" />
        <ImageButton android:id="@+id/btn_showAll"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="Detailed Show" android:layout_gravity="center"
            android:src="@drawable/cmd_result_details" android:background="@android:color/transparent" />
    </TableRow>
</TableLayout>

祝您愉快!


0

您已将ImageButtons放置在tablerow中。tablerow将始终占据屏幕的第一层。您是否希望按钮在顶部位置居中对齐?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接