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

在这里插入图片描述

<think>嗯,用户想修改AlertDialog按钮文字的颜色,这个需求其实在Android开发中挺常见的。我之前处理过类似的样式定制问题,但现在需要找到正确的解决方案。让我想想该怎么做。用户提供的引用信息挺丰富的,里面有自定义AlertDialog主题的方法,但似乎没有直接提到修改按钮文字颜色的部分。比如引用[1]中的CustomAlertDialogTheme应该是用于整体样式设定的。引用[2]建议使用support库的AlertDialog,这是个很好的兼容性建议。按钮文字颜色的设置通常属于主题属性配置的范畴。在Android中,像这样影响控件外观的属性都会定义在主题里。我回忆起来了,Button控件使用的文字颜色主题属性应该是textColorPrimary或者类似的属性。但AlertDialog里的按钮样式可能会用到特定的主题属性。让我仔细思考下:AlertDialog按钮实际上是通过定义在系统主题里的样式控制的。关键点在于要自定义一个主题,然后覆盖AlertDialog相关的样式属性。具体需要修改的属性可能是buttonBarButtonStyle或者alertDialogTheme。为了确保兼容性,确实应该使用AppCompat库。需要创建一个新的样式文件来继承AppCompat的Dialog主题。在这个过程中,还需要考虑不同Android版本下的表现差异,这点很关键。代码实现方面,我觉得需要做三步:先自定义主题,再在AlertDialog.Builder中应用,最后要留意positive和negative按钮可能共用同一个样式属性。如果在主题层面修改,所有按钮颜色会一致改变。如果用户需要单独设置不同按钮的颜色,可能需要额外处理。对了,引用[4]提到了一些系统控件在选中状态使用的颜色配置,这表明主题属性的修改会影响多个控件。所以我的思路应该是对的-修改主题属性是最根本的解决方案。考虑到用户可能需要具体的代码示例,我应该给出一个清晰的自定义主题样例,包括如何设置按钮文字颜色属性的XML代码。同要建议在应用全局主题中引用这个自定义对话框主题。这样梳理下来,解决方案就比较完整了。关键点就是通过主题属性控制按钮文字样式,利用Android主题覆盖机制来实现定制需求。应该能解决用户的问题。</think> 在Android中修改`AlertDialog`按钮文本颜色,需通过**自定义主题覆盖系统样式属性**实现。以下是详细步骤和代码示例: --- ### 1. 自定义对话框主题 在`res/values/styles.xml`中定义主题,覆盖按钮文本颜色属性: ```xml <style name="CustomAlertDialogTheme" parent="ThemeOverlay.AppCompat.Dialog.Alert"> <!-- 修改按钮文字颜色 --> <item name="buttonBarPositiveButtonStyle">@style/CustomDialogButtonStyle.Positive</item> <item name="buttonBarNegativeButtonStyle">@style/CustomDialogButtonStyle.Negative</item> </style> <style name="CustomDialogButtonStyle.Positive" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog"> <item name="android:textColor">@color/green_500</item> <!-- 确定按钮颜色 --> </style> <style name="CustomDialogButtonStyle.Negative" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog"> <item name="android:textColor">@color/red_500</item> <!-- 取消按钮颜色 --> </style> ``` --- ### 2. 应用自定义主题 在代码中通过`ContextThemeWrapper`应用主题: ```java AlertDialog.Builder builder = new AlertDialog.Builder( new ContextThemeWrapper(context, R.style.CustomAlertDialogTheme) ); builder.setTitle("提示") .setMessage("自定义按钮颜色的对话框") .setPositiveButton("确定", null) .setNegativeButton("取消", null) .show(); ``` --- ### 3. 关键说明 1. **核心属性**: - `buttonBarPositiveButtonStyle`:确定按钮样式 - `buttonBarNegativeButtonStyle`:取消按钮样式 - `android:textColor`:直接控制文本颜色 2. **兼容性要求**: - 必须使用`AppCompat`主题(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值