Reputation: 2292
I'd like to place an ImageView
behind an ImageButton
. What's the best way to go about doing this?
Upvotes: 1
Views: 3974
Reputation: 9375
The easiest way is to use an ImageButton
only.
ImageButton
has a background
attribute for the background image
and a src
attribute for the image in the foreground.
See this existing example on StackOverflow.
<ImageButton
android:id="@+id/ImageButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/album_icon"
android:background="@drawable/round_button" />
Upvotes: 0
Reputation: 5057
Alternatively you can use a RelativeLayout
like matiash said:
Here's an example code for a custom header for a navigation drawer containing your profile facebook picture
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
android:id="@+id/customHeader"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/green_gradient"/>
<com.facebook.widget.ProfilePictureView
android:id="@+id/leftPic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
facebook:preset_size="small"
android:paddingBottom="4dp"
android:paddingLeft="3dp"
/>
</RelativeLayout>
RESULT
You can add an ImageButton where you would like, just add the following code into your layout
<ImageButton
android:id="@+id/myImageButton
android:layout_width="SET YOUR WIDTH"
android:layout_height="SET YOUR HEIGHT"
android:background="ADD YOUR BACKGROUND" />
Upvotes: 0
Reputation: 2026
I believe the easiest way to do this is to wrap both the ImageView
and ImageButton
in a FrameLayout
. eg:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/your_image" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" />
</FrameLayout>
Upvotes: 3
Reputation: 55340
You can overlap views in certain layouts (e.g. FrameLayout
, RelativeLayout
).
Just place both views in the xml layout, so that the overlap (e.g. for FrameLayout
, both with width and height as match_parent
). The last one you add goes on top.
Upvotes: 1