【问题标题】:AlertDialog with custom content view looks nothing like AlertDialog带有自定义内容视图的 AlertDialog 看起来不像 AlertDialog
【发布时间】:2012-05-26 22:49:40
【问题描述】:

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

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

我有自己的布局来显示为内容视图,但我想要常规的 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占满整个屏幕:

那么我做错了什么,我应该如何做对呢?

谢谢!

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

【问题讨论】:

  • 如果你想要的只是AlertDialog 的默认外观但有一个列表,我不明白你为什么不直接使用AlertDialog.Builder 并调用setItems(...)。它将为您创建一个列表。
  • 另外,您可以使用AlertDialog.Builder 中的setView() 方法来传递自定义布局。
  • 我正在编辑我的问题来回答你们俩

标签: java android android-alertdialog android-dialog


【解决方案1】:

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

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

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

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

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

Theme_Dialog 状态的文档 (v2.2):

对话框窗口和活动的默认主题(在 API 级别 10 和 较低),由 Dialog 类使用。这会将窗口更改为 浮动(不填满整个屏幕),并在其周围放置一个框架 内容。如果您愿意,可以在活动上设置此主题 制作一个看起来像 Dialog 的 Activity。

希望这会有所帮助!

编辑:

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

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

【讨论】:

  • 好的!它definetley有帮助!现在列表应该在一个浮动框架内,但仍然没有标题......对此有什么想法吗?
  • 我自己结束了测试,看到了setTitle的问题。关于这个有几个相关的问题,最有用的是this one
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-01
  • 1970-01-01
  • 2018-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多