代码之家  ›  专栏  ›  技术社区  ›  Ahmet B.

如何使用AlertDialog在屏幕中央显示自定义布局,而无需任何背景

  •  0
  • Ahmet B.  · 技术社区  · 4 年前

    我想得到一个警报对话框视图时,按钮点击像这样 enter image description here

    enter image description here

    <?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">
    
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/popup_img"
        android:contentDescription="@string/todo" />
    
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="32dp"
        android:orientation="vertical">
    
       // TextView's
    
    </LinearLayout>
    
    
    <FrameLayout
        android:id="@+id/btn_ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|bottom">
    
        <ImageView
            //  />
    
        <TextView
            //     />
    
    </FrameLayout>
    
    
    </FrameLayout>
    

    然而在最后,当我点击按钮时,我得到了这个图像;

    enter image description here

    我的AlertDialog代码是;

         AlertDialog.Builder builder = new AlertDialog.Builder(this);
            View view = getLayoutInflater().inflate(R.layout.layout_congratulations, null);
            builder.setView(view);
            AlertDialog alert = builder.create();
            alert.show();
    
    1 回复  |  直到 4 年前
        1
  •  1
  •   AskNilesh    4 年前

    试试这个,只要改变你的根框架布局的宽度

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="wrap_content">
    

    样本代码

        <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/popup_img"
        android:contentDescription="@string/todo" />
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="32dp"
        android:orientation="vertical">
    
       // TextView's
    
    </LinearLayout>
    
    
    <FrameLayout
        android:id="@+id/btn_ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|bottom">
    
        <ImageView
            //  />
    
        <TextView
            //     />
    
    </FrameLayout>
    
    
    </FrameLayout>
    
        2
  •  1
  •   Aashit Shah    4 年前

    嘿,因为你已经把内容放在中间了,你可以把对话框的窗口设置成透明的,然后显示对话框来去掉白色的背景。

    alert.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    alert.show();