Android: центр выравнивания ImageButtons - PullRequest
5 голосов
/ 16 марта 2011

У меня есть 2 ImageButons, как это

enter image description here

Я перепробовал все, чтобы он выровнялся по центру экрана. Будучи новичком в 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>

Ответы [ 3 ]

6 голосов
/ 16 марта 2011

Надеюсь, у вас есть изображения для этих кнопок напрямую. Так что этот атрибут 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>
0 голосов
/ 16 марта 2011

Вы поместили кнопки ImageButters внутри стола.Столик всегда будет занимать первый слой экрана.Вы хотите, чтобы кнопки были выровнены по центру, но в верхнем положении?

0 голосов
/ 16 марта 2011
    <?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>

Наслаждайтесь !!

...