[英]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 您如何在android中获得黑色主题对话框,如android指南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: 从API 11开始很简单:
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: API 14中添加了字段THEME_DEVICE_DEFAULT_DARK
,因此,如果您在此之前定位,则只需使用数字值即可,因此:
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. 在API 14之前的版本中,您仍然会收到白色警报。
---------------------------------------------------------------- UPDATE -------------------------------------------------------- -------------------------------------------------- -------------- 更新 ----------------------------------- ---------------------
AlertDialog.THEME_DEVICE_DEFAULT_DARK
is depreciated, AlertDialog.THEME_DEVICE_DEFAULT_DARK
已弃用,
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
您可以选择其他主题代替此android.R.style.Theme_DeviceDefault_Light_Dialog_Alert
res/values/styles.xml 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 AndroidManifest.xml
<activity android:name=".ActivityMain"
android:theme="@style/default_activity_theme"/>
如果您不想更改Activity
的主题,则可以扩展AlertDialog
并在其构造Theme.Holo
中提供Theme.Holo
: AlertDialog(Context context, int theme)
。
In case of using DialogFragment
to implement customised dialog, set the theme in onCreate()
method like this: 如果使用DialogFragment
实现自定义对话框,请在onCreate()
方法中设置主题,如下所示:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Holo_Dialog)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.