【问题标题】:AlertDialog theme problemsAlertDialog 主题问题
【发布时间】:2012-10-14 20:50:04
【问题描述】:

我正在创建一个 AlertDialog。如果像这样创建它:

AlertDialog.Builder builder = AlertDialog.Builder((RelationActivity)getContext());
builder.setMessage("No relations found.");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {       
    public void onClick(DialogInterface dialog, int id) {
        ((RelationActivity)getContext()).finish();
    }
});
builder.create();
builder.show();

这是结果:http://www.ozze.com.br/1.png

但是,如果我尝试设置一个主题,像这样:

AlertDialog.Builder builder = new AlertDialog.Builder(((RelationActivity)getContext()), android.R.style.Theme_Holo_Light_Dialog);

这是结果:http://www.ozze.com.br/2.png

请问,谁能帮我解决这个问题?看起来在使用主题时,主题“包围”了警报对话框。

【问题讨论】:

  • 这两行在我看来都一样...
  • 我可以在第二张图片上看到来自主题的不同字体
  • 详细说明您到底想要什么
  • 抱歉,我刚刚编辑了第一个代码...我粘贴了错误的代码。现在我的问题没问题。
  • @Marcin S,不仅仅是字体。设置主题时,警报对话框后面会出现一个白框。

标签: android


【解决方案1】:

要为像 Theme.Holo.Light 这样的警报对话框设置不同的主题,请尝试使用 ContextThemeWrapper,就像在 android 源代码中的 Dialog.java 中使用的那样:

builder = new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_Holo_Light_Dialog))

【讨论】:

  • ContextThemeWrapper 是否适用于 API 14,我怀疑。
  • 谢谢马尔辛!这样可行!但它适用于 Theme_Holo,而不适用于 Theme_Holo_Light……这常见吗?
  • @AndroidStack 它可从 API 1 获得,并且未被弃用。
  • 嗯,Marcin 的解决方案效果很好!但我猜 Theme_Holo_Light 只能在 API 级别 14 上完美运行。一旦我完成当前项目,我将运行一些测试。
  • 很高兴它对您有用。我认为你是对的,并且从 API 11 开始就可以使用 Holo.Light 检查此链接stackoverflow.com/questions/9681648/… 它解释了如何在旧设备中使用 Holo.Light。然后在 ContextThemeWrapper 中,只需将 android.R.style.Theme_Holo_Light_Dialog 替换为您自己的样式,父级是 Holo.Light,如下所示:R.style.myCustomDialog。
【解决方案2】:

这里是原始答案here的链接

为了快速参考,我在这里发布
带有 v7 库的主题 android.support.v7.app.AlertDialog

android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(this,R.attr.alertDialogTheme);

带有 android.app.AlertDialog

构造器的主题
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this,AlertDialog.THEME_HOLO_LIGHT );

但根据新文档
此常量 (AlertDialog.THEME_HOLO_LIGHT) 在 API 级别 23 中已弃用。使用 Theme_Material_Light_Dialog_Alert 。

 android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this,android.R.style.Theme_Material_Light_Dialog_Alert );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-08
    • 2011-10-17
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2011-01-26
    相关资源
    最近更新 更多