AlertDialog的标题居中,底部按钮为三个时居中布局,更改按钮文字默认大写的设置

更改底部按钮默认大写的设置

<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);

底部按钮为三个时,三个按钮居中

//默认情况下:
        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();

在这里插入图片描述
可以看到底部的三个按钮不是居中的。Negative按钮偏右。可以做如下调整:

        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);

在这里插入图片描述

标题居中

		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)
        //其它同上

在这里插入图片描述

发布了37 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/jiejingguo/article/details/90484616