Activity设置了theme导致AlertDialog按钮文字不显示问题

文章介绍了在自定义主题中因字体颜色设置为白色导致AlertDialog按钮文字不可见的问题,提出了两种解决方案:一是手动设置对话框按钮的文字颜色;二是创建自定义风格应用于AlertDialog,通过修改colorPrimary等属性来改变按钮颜色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

	<activity
	   android:name=".modules.other.SplashActivity"
	   android:exported="true"
	   android:screenOrientation="portrait"
	   android:theme="@style/SplashBackgroundStyle">
	</activity>
    <style name="SplashBackgroundStyle" parent="Theme.MaterialComponents.Light.NoActionBar">
        <item name="android:windowBackground">@drawable/app_launch_bg</item>
        <item name="android:windowFullscreen">true</item>
        <item name="colorPrimary">@color/white</item>
        <item name="colorPrimaryVariant">@color/white</item>
        <item name="colorOnPrimary">@color/white</item>
    </style>

原因:
自定义主题 中设置了字体颜色为白色,导致AlertDialog按钮文字显示不出来,但是按钮功能是在的。

方法一:

在show()之后, 设置按钮字体颜色。

     AlertDialog dialog = new AlertDialog.Builder(nActivity).setCancelable(false)
             .setTitle("提示")
             .setMessage("安装应用需要打开未知来源权限,请去设置中开启权限")
             .setNegativeButton("取消", null)
             .setPositiveButton("立即开启", new DialogInterface.OnClickListener() {
                 @Override
                 public void onClick(DialogInterface dialogInterface, int i) {
                     startInstallPermissionSettingActivity();
                 }
             }).create();
     dialog.show();
     
     // 设置按钮字体颜色
     Button btnPos = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
     btnPos.setTextColor(Color.RED);
     Button btnNeg = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
     btnNeg.setTextColor(Color.BLACK);

方法二:

AlertDialog使用自定义的style。

<style name="MyDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
	<item name="colorPrimary">@color/colorPrimary</item>
	<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
	<item name="colorAccent">@color/colorAccent</item>
</style>
AlertDialog dialog = new AlertDialog.Builder(nActivity, R.style.MyDialog).setCancelable(false)
        .setTitle("提示")
        .setMessage("安装应用需要打开未知来源权限,请去设置中开启权限")
        .setNegativeButton("取消", null)
        .setPositiveButton("立即开启", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                startInstallPermissionSettingActivity();
            }
        }).create();
dialog.show();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值