AlertDialog-Fast-Styling
Some visual parts of android.support.v7.AlertDialog can be fastly styled by inheriting its base theme instead of customizing new layouts, which is considered clumsy. Modifying these visual parts still agrees with Material Design.
Requirement
dependencies {
compile 'com.android.support:appcompat-v7:22.2.0'
}
Approach
-
Fork the following style xml code into your
<style name="AlertDialogFastStyling" parent="Theme.AppCompat.Light.Dialog.Alert"> @color/abc_primary_text_material_light @style/AlertDialogFastStyling.Title @drawable/abc_dialog_material_background_light 24dp 24dp @style/AlertDialogFastStyling.Button @drawable/abc_btn_radio_material @drawable/abc_btn_check_material </style>styles.xmland modify their value as you want.<style name="AlertDialogFastStyling.Title" parent="RtlOverlay.DialogWindowTitle.AppCompat"> <item name="android:textAppearance">@style/AlertDialogFastStyling.Title.TextAppearance</item> </style> <style name="AlertDialogFastStyling.Title.TextAppearance"> <item name="android:textSize">@dimen/abc_text_size_title_material</item> <item name="android:textColor">@color/abc_primary_text_material_light</item> </style> <style name="AlertDialogFastStyling.Button" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog"> <item name="android:textColor">#009688</item> <item name="android:textSize">14sp</item> </style> -
Use
AlertDialogFastStylingwhile buildingAlertDialogcomponent. The following samples are available for testing and their insets show the visual mappings to style items inAlertDialogFastStyling.
Confirm Dialog
AlertDialog dialog = new AlertDialog.Builder(context, R.style.AlertDialogFastStyling)
.setTitle("提醒")
.setPositiveButton("是", null)
.setNegativeButton("否", null)
.setMessage("检测到距离您上次清理已超过30天,是否立即清理?")
.show();
===
Choice Dialog
AlertDialog dialog = new AlertDialog.Builder(context, R.style.AlertDialogFastStyling)
.setItems(new String[] {"布丁味的老人茶", "奶昔", "巴拿拿牌烤香蕉"},
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.show();
===
Radio Dialog
AlertDialog dialog = new AlertDialog.Builder(context, R.style.AlertDialogFastStyling)
.setSingleChoiceItems(new String[] {"布丁味的老人茶", "奶昔", "巴拿拿牌烤香蕉"}, 0, null)
.setPositiveButton("确定", null)
.setNegativeButton("取消", null)
.show();
===
Check Dialog
AlertDialog dialog = new AlertDialog.Builder(context, R.style.AlertDialogFastStyling)
.setMultiChoiceItems(new String[] {"布丁味的老人茶", "奶昔", "巴拿拿牌烤香蕉"},
new boolean[] {false, true, true},
null)
.setPositiveButton("确定", null)
.setNegativeButton("取消", null)
.show();



