Android:居中对齐 ImageButtons

标签 android android-layout imagebutton tablelayout

我有 2 个像这样的 ImageButon

enter image description here

我已尽一切努力使它在屏幕中央正确对齐。作为 android 的新手,我不知道该怎么做。我怎样才能实现将这两个图像按钮对齐在屏幕中央的目标

这是我的布局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>

最佳答案

我希望你直接有这些按钮的图片。所以这个设置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>

关于Android:居中对齐 ImageButtons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5321252/

相关文章:

android - 自定义 SurfaceView 和默认 android View 之间的通信

android - 由于 View 测量导致布局性能不佳?

来自 url 的 Android ImageButton

android - android.animation 和 android.transition 包之间的主要区别是什么?

android - 如何以及何时使用通过 acquireUnstableContentProviderClient 获得的 ContentProviderClient?

java - 在 WebView 中启用 JavaScript 后,Recaptcha 无法正常工作

android - 查找在 ScrollView 中被点击的坐标

java - 为什么在某些事件中调用 getLayoutParams() 会抛出异常?例如,onSaveInstanceState

带有图像和文本的 Android ImageButton

java - 移动拖放 ImageButton 时 ImageButtons 被压扁