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

EGH*_*HDK 2 java eclipse android

所以我一直在寻找一个半小时,但我无法弄清楚.我不想设置自定义主题.我想设置一个内置到Android的AlertDialog主题.

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

public AlertDialog.Builder (Context context)
Run Code Online (Sandbox Code Playgroud)

或这个

public AlertDialog.Builder (Context context, int theme)
Run Code Online (Sandbox Code Playgroud)

所以我这样做,它完美地工作:

AlertDialog action_btn = new AlertDialog.Builder(MyActivity.this).create();
Run Code Online (Sandbox Code Playgroud)

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

AlertDialog action_btn = new AlertDialog.Builder(MyActivity.this, AlertDialog.THEME_TRADITIONAL).create();
Run Code Online (Sandbox Code Playgroud)

如果有人可以帮我解决如何设置主题的话,我仍然是编程的新手.

我还有一个奖金问题:

我不能只是AlertDialog()为了工作,让它工作我需要输入AlertDialog.Builder(),但在开发人员网站上,他们似乎都有相同的方法和构造函数.有什么区别/为什么不起作用AlertDialog()

Dan*_*eón 13

将主题包装到上下文中,这可以从API级别1开始提供.

Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(
    this,android.R.style.Theme_DeviceDefault_Light_Dialog));
Run Code Online (Sandbox Code Playgroud)

  • 如果您在使用低于API 11的API时正在寻找对话框样式的解决方案,那么就是这样 (2认同)