java - 如何在 Android 的 AlertDialog 中设置主题

标签 java android eclipse

所以我一直在寻找大约一个半小时,但我无法弄清楚。我不想设置自定义主题。我想为 AlertDialog 设置一个内置在 android 中的主题。

根据 d.android.com 我可以这样做:

public AlertDialog.Builder (Context context)

或者这个

public AlertDialog.Builder (Context context, int theme)

所以我这样做并且效果很好:

AlertDialog action_btn = new AlertDialog.Builder(MyActivity.this).create();

但是,当我需要添加主题时,我从 eclipse 中得到一个错误:

AlertDialog action_btn = new AlertDialog.Builder(MyActivity.this, AlertDialog.THEME_TRADITIONAL).create();

我对编程还是很陌生,如果有人可以帮助我设置主题,我将不胜感激。

我还有一个奖励问题:

我无法让 AlertDialog() 正常工作,要使其正常工作,我需要键入 AlertDialog.Builder(),但在开发者网站上它们都似乎具有相同的方法和构造函数。有什么区别/为什么 AlertDialog() 不能正常工作?

最佳答案

将主题包装到上下文中,这从 API 级别 1 开始可用。

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

关于java - 如何在 Android 的 AlertDialog 中设置主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9448147/

相关文章:

java - 在 Java 中使用方法、setter 和 getter

java - 程序如何读取unicode?

java - 在Java Android应用程序中获取ruby环境变量DATABASE_URL

eclipse - Grails编译器不匹配

java - 从字符串中获取运算符符号

java - 如何使用junit测试Android sqlite?

java - 双变量加减 12%

java - 如何在 OnButtonClick 上显示 ProgressBar 5 秒,然后显示 TextView?

Android Studio - 离线 Maven 存储库中缺少 Gradle 插件

java - 纹理未绘制在 Box2d Body 上