java - 具有自定义内容 View 的 AlertDialog 看起来与 AlertDialog 完全不同

标签 java android android-alertdialog android-dialog

我有一个 AlertDialog 的自定义子类,它应该显示范围内所有可用 Wifi 网络的列表。

我通过创建该对话框的实例并调用 show() 来显示此对话框,并且我没有使用 AlertDialog.Builder (因为我希望使用我的自定义类)。

我有自己的布局来显示为内容 View ,但我想要常规的 AlertDialog 外观和感觉,以及标题标题和框架。

我的自定义布局非常简单:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
</ListView>

我将其添加到 onCreate() 的对话框中:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle(R.string.pick_wifi_network);
    setContentView(R.layout.pick_wifi_dialog);
    // Rest of implementation
}

但结果看起来一点也不像 AlertDialog。没有标题,ListView占据整个屏幕:

enter image description here

那么我做错了什么以及我应该如何做呢?

谢谢!

编辑: 为什么我不使用AlertDialog.Builder:我的自定义Dialog类负责监听WifiManager的SCAN_RESULTS_AVALIABLE_ACTION,并在结果刷新时更新ListView。因此我无法使用 AlertDialog.Builder。 结束编辑

最佳答案

我认为您的问题在于对话框的主题(您没有应用)。

我还没有尝试过这个,但如果我理解正确的话,它应该可以工作:

在对话框的构造函数中,调用接收主题的 super 构造函数,并传递标准对话框主题。

public CustomDialog(Context context) {
    super(context, android.R.style.Theme_Dialog);
}

在 Android 中创建的对话框具有此默认主题。

Theme_Dialog 的文档指出 (v2.2):

Default theme for dialog windows and activities (on API level 10 and lower), which is used by the Dialog class. This changes the window to be floating (not fill the entire screen), and puts a frame around its contents. You can set this theme on an activity if you would like to make an activity that looks like a Dialog.

希望这有帮助!

编辑:

要解决setTitle的问题,最简单的方法似乎是继承Dialog而不是AlertDialog

同样这样做,不需要在构造函数中传递 android.R.style.Theme_Dialog (显然子类化 AlertDialog 没有主题)。

关于java - 具有自定义内容 View 的 AlertDialog 看起来与 AlertDialog 完全不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10668269/

相关文章:

java - 按钮音效仅起作用28次

android - 如何在关闭 AlertDialog 时调用函数?

java - Java中获取 "eclipse-workspace"路径

Android 从命令行停止模拟器

java - 如何使我的 ActionBar DrawerToggle 按钮正常工作?

android - Fragment 的 onCreateView() 方法中 getActivity() 返回 null

java - 无法在 android 中启动 AlertDialog 框

Android 支持库 22.1 和 AlertDialog 错误?

java - 循环每个元素并执行其自己的操作,然后将其存储在 list<String> 中

java - 安卓 OpenGL : IllegalArgumentException use native order direct buffer