1.使用第三方库,如 https://github.com/fengdai/AlertDialogPro
2.使用兼容库的主题
修改values/style.xml ,设置如下:
<!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="alertDialogTheme">@style/AppTheme.Dialog.Alert</item> </style> <style name="AppTheme.Dialog.Alert" parent="Theme.AppCompat.Light.Dialog.Alert"> </style>
在代码中引入类(非常重要):
import android.support.v7.app.AlertDialog;显示对话框:
new AlertDialog.Builder(MainActivity.this) .setTitle("title") .setMessage("message") .setNegativeButton("cancel",null) .setPositiveButton("ok",null) .create() .show();
结果如下: