Проблемы с ImageButton в относительном макете в приложении для Android - PullRequest
2 голосов
/ 07 июля 2011

В моем приложении я хочу разместить 5 кнопок с изображениями внизу страницы. В 5 кнопках я хочу, чтобы две кнопки были в правом и левом углу экрана, и одна кнопка точно в центре экрана. Две другие кнопки должны быть расположены между этими кнопками, и между ними должно быть одинаковое пространство. Я хочу, чтобы это было общим для всех устройств.

Вот мой макет:

<RelativeLayout  android:id="@+id/RelativeLayout01" android:layout_width="fill_parent"  
                 xmlns:android="http://schemas.android.com/apk/res/android"  
                 android:layout_height="wrap_content">    

    <RelativeLayout android:background="#ffffff" android:layout_width="fill_parent" android:layout_alignParentBottom="true" android:id="@+id/relativeLayout1" android:layout_height="wrap_content">
            <ImageButton android:layout_alignParentLeft="true" android:id="@+id/widget32" android:layout_width="wrap_content" android:background="@drawable/back" android:layout_height="wrap_content">
            </ImageButton>

            <ImageButton android:layout_toRightOf="@+id/widget32" android:id="@+id/widget33" android:layout_width="wrap_content" android:background="@drawable/thumbsup" android:layout_height="wrap_content">
            </ImageButton>

            <ImageButton android:layout_centerInParent="true"android:layout_toRightOf="@+id/widget33" android:id="@+id/flipme" android:layout_width="wrap_content" android:background="@drawable/flip" android:layout_height="wrap_content">
            </ImageButton>

            <ImageButton android:layout_toRightOf="@+id/flipme" android:id="@+id/widget35" android:layout_width="wrap_content" android:background="@drawable/thumbsdown" android:layout_height="wrap_content">
            </ImageButton>

            <ImageButton android:layout_alignParentRight="true" android:id="@+id/widget36" android:layout_width="wrap_content" android:background="@drawable/forward" android:layout_height="wrap_content">
            </ImageButton>
    </RelativeLayout>  

   </RelativeLayout>

Я хочу установить все эти изображения в порядке, пожалуйста, помогите мне.

Теперь у меня новая проблема. Я попытался запустить этот код на моих устройствах версий 1.5 и 2.1. В 2.1 раскладка работает нормально, но в устройстве версии 1.5 кнопки расположены в зигзагообразном порядке. Почему это так? Пожалуйста, помогите мне.

Я не могу изменить это на любой другой макет, потому что это повлияет на остальные мои коды. Поэтому, пожалуйста, дайте мне решение в самом Относительном макете.

Ответы [ 2 ]

2 голосов
/ 07 июля 2011

Вы можете получить то же самое, используя TableLayout

Пример

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TableRow>

        <ImageView
            android:id="@+id/j_elective_02"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/aruba"
            />
            <ImageView
            android:id="@+id/j_elective_01"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/aruba"/>

        <ImageView
            android:id="@+id/j_elective_02"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/aruba"
            />
            <ImageView
            android:id="@+id/j_elective_01"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/aruba"/>

        <ImageView
            android:id="@+id/j_elective_02"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/aruba"
            />
    </TableRow>

</TableLayout>
0 голосов
/ 07 июля 2011

Вместо размещения кнопок в относительной раскладке, поместите кнопки в раскладку таблицы примерно так ...

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"  
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"         
         android:background="@drawable/bottom_bar"
         android:stretchColumns="*">        
        <TableRow>          
        <Button
            android:id="@+id/ImageButton1 "
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ImageButton1 ">
        </Button>           

        <Button
            android:id="@+id/ImageButton2 "
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ImageButton2 ">
        </Button>

        <Button
            android:id="@+id/ImageButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ImageButton3"
            >
        </Button>

        <Button
            android:id="@+id/ImageButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ImageButton4"
            >
        </Button>
        <Button
            android:id="@+id/ImageButton5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ImageButton5"
            >
        </Button>
        </TableRow>
</TableLayout>  

Использование android:stretchColumns="*" в TableLayout выравнивает все ваши столбцы с равными интервалами.

...