版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
在小米9se版本,发现应用升级的对话框按钮都偏向左,讲真一开始就考虑是小米又特立独行了。
下面再介绍下出现情况的解决方案,代码如下,两种处理方案:
if (downloadDialog != null && downloadDialog.isShowing()) {
downloadDialog.dismiss();
}
AlertDialog.Builder builder = new Builder(mContext, R.style.loading_dialog);
builder.setTitle("更新");
final LayoutInflater inflater = LayoutInflater.from(mContext);
View v = inflater.inflate(R.layout.progress, null);
mProgress = (ProgressBar) v.findViewById(R.id.progress);
builder.setView(v);
builder.setNegativeButton("取消", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
interceptFlag = true;
}
});
downloadDialog = builder.create();
downloadDialog.setCancelable(false);
downloadDialog.setCanceledOnTouchOutside(false);
downloadDialog.show();
//必须在downloadDialog.show();之后再进行处理
// AlertDialog按键居中的处理,法一:
final Button mNegativeButton = downloadDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
LinearLayout.LayoutParams mNegativeButtonLL = (LinearLayout.LayoutParams) mNegativeButton.getLayoutParams();
mNegativeButtonLL.gravity = Gravity.CENTER;
// mNegativeButtonLL.width = ViewGroup.LayoutParams.MATCH_PARENT;
mNegativeButton.setLayoutParams(mNegativeButtonLL);
// AlertDialog按键居中的处理,法二:
// LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
// layoutParams.weight = 1.0f;
// layoutParams.gravity = Gravity.CENTER;
// downloadDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setLayoutParams(layoutParams);
更具体,可查看 align AlertDialog buttons to center
此外,若设置标题居中:
标题居中
TextView title = new TextView(this);
title.setText("My Custom Title");
title.setPadding(0, 25, 0, 0);
title.setGravity(Gravity.CENTER);
title.setTextSize(18);
title.setTextColor(Color.BLACK);
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.CustomAlertDialog);
AlertDialog dialog = builder.setCustomTitle(title)
//其它同上
若底部按钮为英文,改大小写时
<style name="CustomAlertDialog" parent="@style/Theme.AppCompat.Light.Dialog.Alert">
<item name="buttonBarButtonStyle">@style/CustomAlertDialogButton</item>
</style>
<style name="CustomAlertDialogButton" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="textAllCaps">false</item> <!--关闭默认大写的设置 -->
</style>
//最后通过构造函数将样式传进去
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.CustomAlertDialog);
举栗子:
看到底部的三个按钮不是居中的,Negative按钮偏右;标题朝左。可以做如下调整:
设置头部:
TextView title = new TextView(this);
title.setText("My Custom Title");
title.setPadding(0, 25, 0, 0);
title.setGravity(Gravity.CENTER);
title.setTextSize(18);
title.setTextColor(Color.BLACK);
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.CustomAlertDialog);
AlertDialog dialog = builder.setCustomTitle(title)
//其它同上
设置按钮居中:
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.CustomAlertDialog);
AlertDialog dialog = builder.setTitle("My Title")
.setMessage("My Message")
.setPositiveButton("Positive", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showToast("Positive");
}
}).setNeutralButton("Neutral", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showToast("Neutral");
}
}).setNegativeButton("Negative", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showToast("Negative");
}
}).create();
dialog.show();
//一定要在dialog.show()之后才能执行下面的代码
Button mNegativeButton = dialog.getButton(AlertDialog.BUTTON_NEGATIVE);
Button mPositiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
Button mNeutralButton = dialog.getButton(AlertDialog.BUTTON_NEUTRAL);
LinearLayout.LayoutParams positiveButtonLL = (LinearLayout.LayoutParams) mPositiveButton.getLayoutParams();
positiveButtonLL.weight = 1;
mPositiveButton.setLayoutParams(positiveButtonLL);
LinearLayout.LayoutParams mNegativeButtonLL = (LinearLayout.LayoutParams) mNegativeButton.getLayoutParams();
mNegativeButtonLL.weight = 1;
mNegativeButton.setLayoutParams(mNegativeButtonLL);
LinearLayout.LayoutParams mNeutralButtonLL = (LinearLayout.LayoutParams) mNeutralButton.getLayoutParams();
mNeutralButtonLL.weight = 1;
mNeutralButton.setLayoutParams(mNeutralButtonLL);