Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't make a view overlay 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 the way I arrange my layout... this is an example of a FrameLayout that I tried:

EDIT: just for simplifying my question : Why doesn't the FrameLayout work as it should? (you can copy this code to your Android Studio and see 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>
like image 377
james Avatar asked Aug 08 '15 08:08

james


People also ask

Why does FrameLayout hold one view?

Frame Layout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other.

Is FrameLayout a ViewGroup?

Android Framelayout is a ViewGroup subclass that is used to specify the position of multiple views placed on top of each other to represent a single view screen. Generally, we can say FrameLayout simply blocks a particular area on the screen to display a single view.

Do fragments need FrameLayout?

It is not necessary to use FrameLayout at all. For example in below code there is no layout taken , and fragment itself only contains single imageview .


2 Answers

I experienced the same issue (of note, if the button is disabled it would not overlay all the other views), and found a fix. Simply wrap the button and image in separate sub-frame layouts.

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

I don't know if this has any negative repercussions, aside from perhaps a bit of inefficiency, but it seems to work fine.

like image 105
InfalibleCoinage Avatar answered Sep 28 '22 08:09

InfalibleCoinage


In the Android 5.0 (API 21) and above, you must add android:elevation into 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>
like image 21
Khoa Vo Avatar answered Sep 28 '22 10:09

Khoa Vo