简体   繁体   中英

How to set theme in AlertDialog in Android

So I've been looking for about an hour and a half, but I can't figure it out. I do NOT want to set a custom theme. I want to set a theme that is built into android for an AlertDialog.

According to d.android.com I can do this:

public AlertDialog.Builder (Context context)

or this

public AlertDialog.Builder (Context context, int theme)

So I do this and it works perfectly:

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

But, when I need to add a theme I get an error from eclipse:

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

I am still very new to programming if someone could help me out on how to set a theme it'd be appreciated.

I also have a bonus question:

I can't get just AlertDialog() to work, to make it work I need to type AlertDialog.Builder() , but on the developer website they both seem to have the same methods and constructors. What's the difference/why doesn't AlertDialog() just work?

Wrap the theme into the context, this is available since API level 1.

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

Themes for the AlertDialog.Builder are only available for Android 3.0 and newer (API level 11). It seems you are have set an earlier Android version in your project settings.

The Android references show the API level of all constructors and methods. You can even set a filter to show you only the methods available for your API level.

Read more about API levels here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM