如何通过使用AlertDialog在屏幕中央显示自定义布局,而不需要任何背景。

问题描述 投票:0回答:1

我想得到一个当按钮被点击时的警报对话框,就像这样。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的代码是

     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();
android android-alertdialog android-view
1个回答
1
投票

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

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

1
投票

嘿既然你已经将内容居中了,你可以将对话框的窗口设置为透明,然后再显示对话框,去掉那个白色背景。

alert.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
alert.show();
© www.soinside.com 2019 - 2024. All rights reserved.