是否可以自定义AlertDialog中的确定和取消按钮?我需要使用自定义样式来替换默认的样式。
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {...
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {...
能有人告诉我如何做到这一点吗?(涉及IT技术)
是否可以自定义AlertDialog中的确定和取消按钮?我需要使用自定义样式来替换默认的样式。
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {...
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {...
public class ComentarDialog extends DialogFragment{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Mensaje de alerta")
.setTitle("Comentar")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("CANCELAR", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
return builder.create();
}
@Override
public void onStart() {
super.onStart();
//Personalizamos
Resources res = getResources();
//Buttons
Button positive_button = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_POSITIVE);
positive_button.setBackground(res.getDrawable(R.drawable.btn_selector_dialog));
Button negative_button = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_NEGATIVE);
negative_button.setBackground(res.getDrawable(R.drawable.btn_selector_dialog));
int color = Color.parseColor("#304f5a");
//Title
int titleId = res.getIdentifier("alertTitle", "id", "android");
View title = getDialog().findViewById(titleId);
if (title != null) {
((TextView) title).setTextColor(color);
}
//Title divider
int titleDividerId = res.getIdentifier("titleDivider", "id", "android");
View titleDivider = getDialog().findViewById(titleDividerId);
if (titleDivider != null) {
titleDivider.setBackgroundColor(color);
}
}
}
button.setBackground()
... 需要 API 级别 16。 - Greg您可以在对话框中设置每个视图。您可以设置带有两个按钮的视图,而不设置积极和消极按钮。
例如:
AlertDialog.Builder builder =
new AlertDialog.Builder(this);
View dialogView = LayoutInflater.from(this)
.inflate(R.layout.my_layout, null);
builder.setView(dialogView);
final Dialog dialog = new Dialog(ThisweekActivity.this, android.R.style.Theme_Translucent_NoTitleBar);
View view = LayoutInflater.from(ThisweekActivity.this).inflate(R.layout.issue_cover_prompt_layout, null);
view.findViewById(R.id.close_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
ImageView img = (ImageView) view.findViewById(R.id.issue_cover_img);
img.setImageBitmap(issue.getCoverImage());
dialog.setContentView(view);
dialog.show();
您可以在对话框中设置布局并添加单击侦听器。