android - 在当前运行的 Activity 上添加一个具有新布局的新对话框,而不更改当前运行的 Activity 的布局文件

标签 android android-layout

如何在不更改当前正在运行的 Activity 的布局文件的情况下,在当前运行的 Activity 上添加具有新布局的新对话框。我无法添加新 Activity ,因为在显示新对话框时旧 Activity 应保持 Activity 状态。

最佳答案

这是一个开始创建 Dialog 并向其应用布局文件的小 fragment 。

    // create an instance of Dialog
    Dialog dialog= new Dialog(c, R.style.CustDialog);  

    //inflate a layout
    LayoutInflater inflater = this.getLayoutInflater();
    View root = inflater.inflate(R.layout.custom_alert, null);

    // set the layout for the Dialog
    dialog.setContentView(root);

如果您阅读 Dialog Docs它提供了您可以使用的不同方法。

注意文档说

The Dialog class is the base class for dialogs, but you should avoid instantiating Dialog directly. Instead, use one of the following subclasses:

因此,您可能需要查看 AlertDialog您可以在 SO 或 Google 上找到大量示例。

This answer给出了创建自定义 Dialog 类的示例。

关于android - 在当前运行的 Activity 上添加一个具有新布局的新对话框,而不更改当前运行的 Activity 的布局文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22775494/

相关文章:

java - 使用 BottomBar 可以防止 fragment 打开?

java - 在代码中一次性设置所有 int/imageresource/clicklistener ext

android - 使用 Android Jelly Bean 的 Nexus 7 内存消耗非常高

android - 在 Android 中使用按钮滑动调整元素大小

android - Google Play 开发者建议平板电脑

android - LinearLayout 未正确调整 ImageView 的大小

android - checkSelfPermission 空指针 6.0

java - 构建使用 Samsung S Pen SDK 2.3 的非常基本的应用程序时出现问题

android - 拍照后获取gps位置

Android:调试触摸、焦点等下的 UI 元素