开发者

ImageButton issues in relative layout in android app

开发者 https://www.devze.com 2023-03-18 10:11 出处:网络
In my app I want to place 5 image buttons in the bottom of the page. In the 5 buttons I want two buttons to be at right and left corner of the screen, and one button exactly at the center of the scree

In my app I want to place 5 image buttons in the bottom of the page. In the 5 buttons I want two buttons to be at right and left corner of the screen, and one button exactly at the center of the screen. The other two buttons must be placed inbetween of these buttons and there must be equal space between them. I want this to be created common for all devices.

The following is my layout:

<RelativeLayout  android:id="@+id/RelativeLayout01" android:layout_width="fill_parent"  
                 xmlns:android="http://schemas.android.com/apk/res/android"  
                 android:layout_height="wrap_c开发者_如何学JAVAontent">    

    <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>

I want to set all these images in an order, please help me.

Now I have a new problem. I tried running this code in my devices of version 1.5 and 2.1. In 2.1, the layout works fine, but in version 1.5 device, the buttons are in a zig zag order. Why is it like this? Please help me.

I can't change this to any other layout because it will affect the rest of my codes. So please give me a solution in Relative layout itself.


You can get the same using TableLayout

Example

<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>


Instead of placing the buttons in the relative layout, put the buttons in the table layout something like this...

<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>  

Using android:stretchColumns="*" in the TableLayout aligns all your columns at equal intervals.

0

精彩评论

暂无评论...
验证码 换一张
取 消