[英]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.我尝试将 imageView 的 layout_marginTop 设置为负值,但图像似乎被截断。
This is what my output looks like.这就是我的 output 的样子。
This is my code in Java这是我在 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 中的代码。
<?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 看看它是否应该是这样的
<?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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.