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

	<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();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要修改框架层的AlertDialog的确认和取消按钮文字颜色,你可以尝试以下步骤: 1. 创建一个自定义的AlertDialog主题,在其中设置按钮文字颜色。在你应用的 `res/values/styles.xml` 文件中添加以下代码: ```xml <style name="CustomAlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert"> <item name="android:buttonBarButtonStyle">@style/CustomButtonBarButtonStyle</item> </style> <style name="CustomButtonBarButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog"> <item name="android:textColor">@color/custom_button_text_color</item> </style> ``` 2. 在你的 `res/values/colors.xml` 文件中定义你想要的自定义按钮文字颜色: ```xml <color name="custom_button_text_color">#FF0000</color> ``` 3. 在使用AlertDialog的地方,将主题设置为你自定义的主题。例如,在Activity中使用: ```java AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.CustomAlertDialogTheme)); builder.setTitle("Title") .setMessage("Message") .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // 点击确认按钮的逻辑 } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // 点击取消按钮的逻辑 } }); AlertDialog dialog = builder.create(); dialog.show(); ``` 通过以上步骤,你可以自定义AlertDialog的确认和取消按钮文字颜色。记得将 `custom_button_text_color` 替换为你想要的颜色值。希望这能帮到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值