简体   繁体   中英

Android ImageView between ImageButtons

I have a RelativeLayout with an ImageButton at either end and an ImageView in the middle. I want the ImageButtons to be the default android size with the ImageView using all the remaining space in the center.

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="left" >

            <ImageButton
                android:id="@+id/btnLeft"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_gravity="left"
                android:onClick="onClick"
                android:src="@drawable/left" />

            <ImageView
                android:id="@+id/imgBoard"
                android:layout_centerHorizontal="true"
                android:layout_centerInParent="true"
                android:layout_centerVertical="true"
                android:scaleType="centerInside"
                android:src="@drawable/ic_launcher" />

            <ImageButton
                android:id="@+id/btnRight"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="0dip"
                android:onClick="onClick"
                android:src="@drawable/right" />

    </RelativeLayout>

You can achieve this by setting the ImageButtons first and then set the ImageView between them by using android:layout_toLeftOf="@id/btnRight" and android:layout_toRightOf="@id/btnLeft"

This will work for you:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageButton
        android:id="@+id/btnLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_gravity="left"
        android:onClick="onClick"
        android:src="@drawable/left" />

    <ImageButton
        android:id="@+id/btnRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="0dip"
        android:onClick="onClick"
        android:src="@drawable/right" />

    <ImageView
        android:id="@+id/imgBoard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@id/btnRight"
        android:layout_toRightOf="@id/btnLeft"
        android:scaleType="centerInside"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM