简体   繁体   中英

How to get the default alert Dialog on android have a black theme

How do you get the black themed Dialog in android like shown on the android guide http://developer.android.com/guide/topics/ui/dialogs.html

I took a screen shot. Whenever I use Alert Dialog, I get the dialog on the left, I want the one on the right.

在此处输入图片说明

It's simple for API 11 onwards:

AlertDialog.Builder alert = new AlertDialog.Builder(context, AlertDialog.THEME_DEVICE_DEFAULT_DARK);

The field THEME_DEVICE_DEFAULT_DARK was added in API 14 so if you are targeting before then, just use the numeric value, thus:

AlertDialog.Builder alert = new AlertDialog.Builder(context, 4);

The different constants you can use, and their values are shown here . On pre API 14 you will still get the white alert though.

---------------------------------------------------------------- UPDATE --------------------------------------------------------

AlertDialog.THEME_DEVICE_DEFAULT_DARK is depreciated,

Below is the updated code:

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault_Light_Dialog_Alert);

You can choose different themes instead of this android.R.style.Theme_DeviceDefault_Light_Dialog_Alert

res/values/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
        <style name="default_activity_theme" parent="@android:style/Theme.Holo"/>
</resources>

AndroidManifest.xml

<activity android:name=".ActivityMain"
          android:theme="@style/default_activity_theme"/>

如果您不想更改Activity的主题,则可以扩展AlertDialog并在其构造Theme.Holo中提供Theme.HoloAlertDialog(Context context, int theme)

In case of using DialogFragment to implement customised dialog, set the theme in onCreate() method like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState)
    setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Holo_Dialog)
}

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