繁体   English   中英

如何使用没有任何背景的 AlertDialog 在屏幕中央显示自定义布局

[英]How to show custom layout in the center of the screen by using AlertDialog without any backround

当像这样单击按钮时,我想获得一个警报对话框视图在此处输入图像描述

我的布局文件在此处输入图像描述

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

但是最后,当我单击按钮时,我得到了这张图片;

在此处输入图像描述

它有白色背景,我不想得到它,而且它没有居中。 谁能帮我? 我应该使用警报对话框吗?

我的 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();

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

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

嘿,因为您已经将内容居中,您可以将对话框的 window 设置为透明,然后显示对话框以删除该白色背景。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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