简体   繁体   中英

Overlay an image on top of custom AlertDialog in Android Studio

I am trying to overlay an image on top of the customized alert dialog just like this one. I tried setting the layout_marginTop of the imageView with negative value but the image appears cut off.

This is what my output looks like.

This is my code in Java

AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
View view = getLayoutInflater().inflate(R.layout.dialog_error, null);

ivError = view.findViewById(R.id.ivError);
lblError = view.findViewById(R.id.lblError);
tvError = view.findViewById(R.id.tvError);

builder.setView(view);
AlertDialog dialog = builder.create();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();

and this is my code in XML.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/round_corner_template"
android:orientation="vertical">

<ImageView
    android:id="@+id/ivError"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_centerHorizontal="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="-60dp"
    android:src="@drawable/pic_error" />

<TextView
    android:id="@+id/lblError"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/ivError"
    android:fontFamily="sans-serif-black"
    android:gravity="center"
    android:text="Error Text"
    android:textSize="15sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/tvError"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/lblError"
    android:layout_marginTop="10dp"
    android:gravity="center"
    android:text="Error Text"
    android:textSize="12sp" />

</RelativeLayout>

Please change your xml like this and see if this is what it should look like

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/round_corner_template"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:orientation="vertical" >
    </LinearLayout>

    <ImageView
        android:id="@+id/ivError"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/pic_error" />

    <TextView
        android:id="@+id/lblError"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ivError"
        android:fontFamily="sans-serif-black"
        android:gravity="center"
        android:text="Error Text"
        android:textSize="15sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tvError"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/lblError"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="Error Text"
        android:textSize="12sp" />

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