我正在使用MaterialAlertDialogBuilder
,因为我想要圆角以及白天和黑夜模式。并且请注意,我不能使用dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
行,因为在我的样式中,style.xml中有一个不同的父项,我用来根据日/夜模式设置颜色。
问题:如何在MaterialAlertDialogBuilder中更改正/负按钮背景颜色?
请注意,可绘制背景在MaterialAlertDialogBuilder
中不起作用
代码:
public void showNotesDialog() {
MaterialAlertDialogBuilder alertDialogBuilder = new MaterialAlertDialogBuilder(this, R.style.dialogBoxStyle);
LayoutInflater li = LayoutInflater.from(this);
View promptsView = li.inflate(R.layout.row_note_layout, null);
alertDialogBuilder.setView(promptsView);
etNote = promptsView.findViewById(R.id.et_note);
String buttonName = getString(R.string.add);
if (!getNotes.isEmpty()) {
buttonName = getString(R.string.update);
etNote.getText().clear();
etNote.setText(getNotes);
}
alertDialogBuilder.setPositiveButton(buttonName, null);
alertDialogBuilder.setNegativeButton(getString(R.string.cancel), null);
alertDialogBuilder.setNeutralButton("Clear", null);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.setCancelable(false);
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Button buttonPositive = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
Button buttonNegative = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEGATIVE);
((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEUTRAL).setTextColor(getResources().getColor(R.color.colorRed));
Button buttonClear = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEUTRAL);
buttonClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
etNote.getText().clear();
getNotes = "";
noteDesc.setText(getNotes);
}
});
buttonPositive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getNotes = etNote.getText().toString();
noteDesc.setText(getNotes);
dialog.dismiss();
}
});
buttonNegative.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
});
alertDialog.show();
}
样式(v21)
<style name="dialogBoxStyle" parent="Theme.AppCompat.DayNight.Dialog.Alert">
<item name="android:background">?attr/day_colorWhite_night_colorVeryDarkBlackMostlyBlack</item>
<item name="android:textColor">?attr/day_colorDarkGray_night_colorWhite</item>
<item name="android:textColorAlertDialogListItem">?attr/day_colorDarkGray_night_colorWhite</item>
<item name="android:textColorSecondary">?attr/day_colorDarkGray_night_colorWhite</item>
<item name="android:textColorPrimary">?attr/day_colorDarkGray_night_colorWhite</item>
<item name="colorAccent">?attr/day_colorDarkGray_night_colorWhite</item>
<item name="android:typeface">normal</item>
<item name="textColorAlertDialogListItem">?attr/day_colorDarkGray_night_colorWhite</item>
<item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay.MyApp.Dialog.Rounded</item>
</style>
<style name="ShapeAppearanceOverlay.MyApp.Dialog.Rounded" parent="">
<item name="cornerFamily">rounded</item>
<!--TODO: Please note that "cornerSize" will give effect on outside of Dialog Box border line, not where the row is inflated.-->
<!--TODO: If you want to set Inside corner: app:cardCornerRadius="60dp"-->
<item name="cornerSize">8dp</item>
</style>
答案 0 :(得分:2)
在对话框中尝试以下代码
alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.DKGRAY)
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.BLUE)
要更改背景,请参阅this post