首页 > 解决方案 > 是否可以替换 AlertDialog 布局?

问题描述

我一直在尝试创建一个自定义的 AlertDialog 并且我知道我可以用来AlertDialog.Builder.setView()在对话框中放置一个自定义视图,但是是否可以完全替换默认布局?

编辑:我想这样做是因为它允许我使用带有自定义布局的构建setMessage()器等setTitle()

标签: androidlayoutdialog

解决方案


可以自定义或完全更改,Dialog或者AlertDialog,您可以Dialog像这样自定义

private void customDialog() {
        final Dialog dialog = new Dialog(ActivityUserVideoPlay.this, R.style.MaterialDialogSheet);
        dialog.setContentView(R.layout.your_layout_foor_dialog); // your custom view.
        dialog.setCancelable(false);
        dialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        dialog.show();
    }

这是对话框的样式

 <style name="MaterialDialogSheet" parent="@android:style/Theme.Dialog">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:windowAnimationStyle">@style/MaterialDialogSheetAnimation</item>
    </style>

使用动画根据需要打开或关闭对话框,否则可以将其删除。

希望能帮助到你。


推荐阅读