如何在 material 警报对话框中设置按钮图标大小
How to set button icon size in material alert dialog
我有简单的 MaterialAlertDialog
,我想为正按钮设置图标。所以我以编程方式设置图标,因为我想要多个警报对话框的唯一图标,并为所有 dialog
使用相同的主题
这是我的做法。
final MaterialAlertDialogBuilder dialogBuilder = new MaterialAlertDialogBuilder(SubListActivity.this, R.style.AlertDialogTheme);
dialogBuilder.setTitle("Delete selected files ?");
ListView modeList = new ListView(SubListActivity.this);
modeList.setPadding(50,50,50,50);
CustomArrayAdapter adapter = new CustomArrayAdapter(SubListActivity.this, temp);
modeList.setAdapter(adapter);
dialogBuilder.setView(modeList);
dialogBuilder.setNegativeButton(android.R.string.no, null);
dialogBuilder.setPositiveButton(R.string.delete, (dialog, which) -> {
//Some function
}).setPositiveButtonIcon(getDrawable(R.drawable.ic_item_delete)).create().show();
主题
<style name="AlertDialogTheme" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
<item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
<item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
</style>
<style name="NegativeButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="rippleColor">@color/primarySubText</item>
<item name="android:textColor">@color/primarySubText</item>
</style>
<style name="PositiveButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Icon">
<item name="rippleColor">@color/secondaryLightColor</item>
<item name="android:textColor">@color/darkRed</item>
<item name="iconTint">@color/darkRed</item>
<item name="iconSize">4dp</item>
<item name="iconPadding">4dp</item>
</style>
结果:
如果我在主题中设置图标,大小适合按钮。但正如我之前提到的,我想为所有对话框设置唯一的图标。
那么如何调整图标大小呢?
fun showAlert(): AlertDialog {
// for example we have drawable
val drawable: Drawable = ContextCompat.getDrawable(activity, R.drawable.positive)
// our material dialog builder
val alertDialogBuilder = MaterialAlertDialogBuilder(activity, R.style.AlertDialogStyle)
/**
then do something here, set title, body, etc..
*/
// get alert dialog from builder
val alertDialog = alertDialogBuilder.create()
// for example, we add drawable to positive button
alertDialog.setOnShowListener {
// now we can get positive button from alertDialog
val positiveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE)
// text size of our button
val textSize = positiveButton.textSize.toInt()
// drawable. we set size here
val positiveDrawableResized = drawable.apply { setBounds(0, 0, textSize, textSize) }
// add drawable to button
positiveButton.setCompoundDrawables(positiveDrawableResized, null, null, null)
// positive button click listener (if needed)
positiveButton.setOnClickListener { alertDialog.dismiss() }
}
// finally, show dialog
alertDialog.show()
return alertDialog
}
我有简单的 MaterialAlertDialog
,我想为正按钮设置图标。所以我以编程方式设置图标,因为我想要多个警报对话框的唯一图标,并为所有 dialog
这是我的做法。
final MaterialAlertDialogBuilder dialogBuilder = new MaterialAlertDialogBuilder(SubListActivity.this, R.style.AlertDialogTheme);
dialogBuilder.setTitle("Delete selected files ?");
ListView modeList = new ListView(SubListActivity.this);
modeList.setPadding(50,50,50,50);
CustomArrayAdapter adapter = new CustomArrayAdapter(SubListActivity.this, temp);
modeList.setAdapter(adapter);
dialogBuilder.setView(modeList);
dialogBuilder.setNegativeButton(android.R.string.no, null);
dialogBuilder.setPositiveButton(R.string.delete, (dialog, which) -> {
//Some function
}).setPositiveButtonIcon(getDrawable(R.drawable.ic_item_delete)).create().show();
主题
<style name="AlertDialogTheme" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
<item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
<item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
</style>
<style name="NegativeButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="rippleColor">@color/primarySubText</item>
<item name="android:textColor">@color/primarySubText</item>
</style>
<style name="PositiveButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Icon">
<item name="rippleColor">@color/secondaryLightColor</item>
<item name="android:textColor">@color/darkRed</item>
<item name="iconTint">@color/darkRed</item>
<item name="iconSize">4dp</item>
<item name="iconPadding">4dp</item>
</style>
结果:
如果我在主题中设置图标,大小适合按钮。但正如我之前提到的,我想为所有对话框设置唯一的图标。
那么如何调整图标大小呢?
fun showAlert(): AlertDialog {
// for example we have drawable
val drawable: Drawable = ContextCompat.getDrawable(activity, R.drawable.positive)
// our material dialog builder
val alertDialogBuilder = MaterialAlertDialogBuilder(activity, R.style.AlertDialogStyle)
/**
then do something here, set title, body, etc..
*/
// get alert dialog from builder
val alertDialog = alertDialogBuilder.create()
// for example, we add drawable to positive button
alertDialog.setOnShowListener {
// now we can get positive button from alertDialog
val positiveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE)
// text size of our button
val textSize = positiveButton.textSize.toInt()
// drawable. we set size here
val positiveDrawableResized = drawable.apply { setBounds(0, 0, textSize, textSize) }
// add drawable to button
positiveButton.setCompoundDrawables(positiveDrawableResized, null, null, null)
// positive button click listener (if needed)
positiveButton.setOnClickListener { alertDialog.dismiss() }
}
// finally, show dialog
alertDialog.show()
return alertDialog
}