【问题标题】:Is it possible to replace AlertDialog layout?是否可以替换 AlertDialog 布局?
【发布时间】:2018-06-08 20:03:40
【问题描述】:

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

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

【问题讨论】:

标签: android layout dialog


【解决方案1】:

可以自定义或完全更改DialogAlertDialog,您可以像这样自定义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>

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

希望对你有帮助。

【讨论】:

  • 这并不能直接回答问题,但创建自定义对话框而不是破解 AlertDialog 是有意义的
  • 它不是任何 Alert Dialog Hack 它也可以作为 AlertDialog 你可以使用任何具有相同属性的对话框
【解决方案2】:

是的,您可以,您只需创建.xml 并对其进行膨胀。就这样:

final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        // Get the layout inflater
        LayoutInflater inflater = this.getLayoutInflater();
        View alertView = inflater.inflate(R.layout.newsletter_dialog, null);

        // Pass null as the parent view because its going in the dialog layout
        builder.setView(alertView);
        final AlertDialog dialog = builder.show();

【讨论】:

  • 这正是我所描述的我不想做的 - 这将视图放置在 AlertDialog 的布局中
猜你喜欢
  • 2015-04-13
  • 2010-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-13
相关资源
最近更新 更多