在Java中 alertdialog如何修改button的内外边距
时间: 2024-02-05 22:03:28 浏览: 80
在Java中,AlertDialog的Button的内外边距可以通过以下方式修改:
1. 创建一个自定义Button样式,设置Button的内外边距。
```
<style name="CustomButton" parent="@android:style/Widget.Button">
<item name="android:padding">10dp</item>
</style>
```
2. 在AlertDialog的构建器中,使用setButton方法设置自定义的Button样式。
```
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Title")
.setMessage("Message")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
AlertDialog dialog = builder.create();
// Set the custom button style for positive and negative buttons
Button positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
positiveButton.setBackgroundResource(R.drawable.custom_button);
positiveButton.setTextColor(ContextCompat.getColor(context, R.color.white));
positiveButton.setPadding(20, 20, 20, 20);
Button negativeButton = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
negativeButton.setBackgroundResource(R.drawable.custom_button);
negativeButton.setTextColor(ContextCompat.getColor(context, R.color.white));
negativeButton.setPadding(20, 20, 20, 20);
dialog.show();
```
在上面的代码中,我们获取AlertDialog的PositiveButton和NegativeButton,并设置其背景、文字颜色和内外边距。通过设置Button的padding属性,我们可以调整Button的内外边距。
阅读全文
相关推荐















