【问题标题】:Android: Center align ImageButtonsAndroid:居中对齐 ImageButtons
【发布时间】:2013-07-22 12:52:07
【问题描述】:

我有 2 个这样的 ImageButton

我已尽一切努力使其在屏幕中心正确对齐。作为android的新手,我没有任何其他线索可以做什么。我怎样才能实现将这 2 个图像按钮对齐在屏幕中心的目标

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

【问题讨论】:

  • 请添加适合您的 cmets,或者您有任何问题...也接受正确答案。

标签: android android-layout imagebutton tablelayout


【解决方案1】:

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

【讨论】:

  • 谢谢..工作得很好。我在两者之间添加了一个空白的 TextView 以在这些图像按钮之间留出一些空隙。谢谢
  • 如果你想添加空间,不需要为此添加文本视图...添加边距属性...就足够了。对于您的第一个按钮,请输入类似 android:layout_marginRight="30dp"... 它会添加边距,因此间距会增加...
【解决方案2】:
    <?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>

享受!!

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-11
      • 1970-01-01
      • 2021-09-26
      • 2015-07-25
      • 2014-09-01
      • 2011-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多