Unable to make an overlay view of another view in FrameLayout

I want to make my ImageView on top of my button, but the button will always overlay the ImageView, no matter which layout I choose, or how I arrange my layout ... this is an example of a FrameLayout that I tried

EDIT: just to simplify my question: Why does FrameLayout not work as it should? (you can copy this code to your Android studio and see for yourself)

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:padding="0.1dp"> <Button android:clickable="false" android:layout_width="40dp" android:layout_height="40dp" android:text="135" android:background="@android:color/holo_blue_bright" android:layout_gravity="center" android:id="@+id/buttonText"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/triangle" android:id="@+id/triangle" android:paddingTop="5dp" android:layout_gravity="center_horizontal|bottom" /> </FrameLayout> 
+4
source share
4 answers

I myself solved this problem by changing Button to TextView . By default, the behavior of FrameLayout does not work, because the Android system always sets buttons for overlaying other views (so that the user can always click the button).

0
source

I had the same problem (note that if the button is disabled, it will not overlap all other views) and will find a fix. Just wrap the button and image in separate subframe layouts.

 <FrameLayout>... <FrameLayout>... <Button ... /> </FrameLayout> <FrameLayout>... <ImageView ... /> </FrameLayout> </FrameLayout> 

I do not know if this has any negative consequences, except, perhaps, a little inefficiency, but it seems to be normal.

+4
source

In Android 5.0 (API 21) and above, you must add android: height to the view.

 <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:padding="0.1dp"> <Button android:clickable="false" android:layout_width="40dp" android:layout_height="40dp" android:text="135" android:background="@android:color/holo_blue_bright" android:layout_gravity="center" android:id="@+id/buttonText"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/triangle" android:id="@+id/triangle" android:paddingTop="5dp" android:layout_gravity="center_horizontal|bottom" android:elevation="3dp" /> </FrameLayout> 
0
source

You can call the bringToFront () method on the view you want to get in front

Example:

 yourView.bringToFront(); 
-one
source

All Articles