本文介绍了更改对话框按钮的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么方法可以更改默认对话框按钮的颜色,还是需要为此自定义对话框?
Is there any way how to change default dialog buttons colour, or do I need to do custom dialog for that?
这是我的对话框:
private void removeItem(final int position) {
/** Create dialog which ask if user is sure about delete name from list **/
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Delete player");
builder.setIcon(R.mipmap.ic_launcher);
builder.setMessage("Do you want to delete player: \"" + mNameList.get(position).getText1() + "\"?")
.setCancelable(false)
/** If user click "ok" button, delete player from list and save changes **/
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mNameList.remove(position);
mAdapter.notifyItemRemoved(position);
saveData();
}
})
/** If user click "cancel" button, name won't delete from list **/
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
/** Create Dialog **/
AlertDialog alert = builder.create();
alert.show();
}
推荐答案
您可以使用 MaterialAlertDialogBuilder
由材质组件库,它允许创建材质AlertDialog
.
You can use the MaterialAlertDialogBuilder
provided by the Material Components library which allows the creation of a Material AlertDialog
.
只需使用:
new MaterialAlertDialogBuilder(MainActivity.this,
R.style.MyThemeOverlay_MaterialComponents_MaterialAlertDialog)
.setTitle("Dialog")
.setMessage("Lorem ipsum dolor ....")
.setPositiveButton("Ok", /* listener = */ null)
.setNegativeButton("Cancel", /* listener = */ null)
.show();
然后使用 buttonBarPositiveButtonStyle
和 buttonBarNegativeButtonStyle
属性定义您的自定义样式:
Then define your custom style, using the buttonBarPositiveButtonStyle
and buttonBarNegativeButtonStyle
attributes:
<!-- Alert Dialog -->
<style name="MyThemeOverlay.MaterialComponents.MaterialAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
<item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
<item name="buttonBarNeutralButtonStyle">....</item>
</style>
<style name="PositiveButtonStyle" parent="@style/Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:textColor">#FFFFFF</item>
<item name="backgroundTint">#00f</item>
</style>
<style name="NegativeButtonStyle" parent="@style/Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:textColor">@color/colorAccent</item>
<item name="backgroundTint">@color/secondaryColor</item>
</style>
这篇关于更改对话框按钮的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!