簡體   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