繁体   English   中英

在 Android Studio 中的自定义 AlertDialog 顶部覆盖图像

[英]Overlay an image on top of custom AlertDialog in Android Studio

我正在尝试将图像叠加在自定义警报对话框的顶部,就像这个一样。 我尝试将 imageView 的 layout_marginTop 设置为负值,但图像似乎被截断。

这就是我的 output 的样子。

这是我在 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();

这是我在 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>

请像这样更改您的 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM