【问题标题】:Android alert dialogue styling not working on samsung 6.0.1Android警报对话样式不适用于三星6.0.1
【发布时间】:2017-08-08 15:03:36
【问题描述】:

我在 android 中创建了 android 对话框的样式 & 代码粘贴在下面。但该样式似乎不适用于 Android 6.0.1,三星手机。我也共享了两个屏幕截图。请帮助我。

代码

<style name="GeneraButton1" parent="android:Widget.Button">
    <item name="android:textColor">@color/colorPrimary</item><item name="android:fontFamily">sans-serif</item><item name="android:textSize">@dimen/key_text</item><item name="android:gravity">center</item><item name="android:background">@drawable/general_button1_selector</item>
</style>

<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <!-- Used for the buttons --><item name="colorAccent">@color/white</item><!-- Used for the title and text --><item name="android:textColorPrimary">@color/colorPrimary</item><!-- Used for the title and text --><item name="android:textColor">@color/colorPrimary</item><!-- Used for the background --><item name="android:background">@color/white</item><item name="android:button">@style/GeneraButton</item><item name="android:actionButtonStyle">@style/GeneraButton</item>
</style>

Java代码

public void showAlertDialog(String title, String message, IAlertDialogueInterface handler, final View view, boolean setPositiveButton, int posBtnString, int negBtnStr) {
    final IAlertDialogueInterface internalHandler;
    if (handler == null) {
        internalHandler = new AlertHandler();
    } else {
        internalHandler = handler;
    }
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this, R.style.MyAlertDialogStyle);
    alertDialogBuilder.setTitle(title);
    alertDialogBuilder.setMessage(message);
    alertDialogBuilder.setCancelable(false);
    if (setPositiveButton) {
        alertDialogBuilder.setPositiveButton(this.getString(posBtnString),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    alertDialog.dismiss();
                    internalHandler.onAlertButtonClick(arg1, view);
                }
            });
    }
    alertDialogBuilder.setNegativeButton(this.getString(negBtnStr), new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            alertDialog.dismiss();
            internalHandler.onAlertButtonClick(which, view);
        }
    });

    alertDialog = alertDialogBuilder.create();

    if (view != null) {
        alertDialog.setView(view);
    }
    alertDialog.show();
}

MotoG4Plus(Android 7.0 屏幕截图)

三星(Android 6.0.1 屏幕截图)

【问题讨论】:

  • 您只有一个 style.xml 文件?我的意思是你没有 na v23/styles.xml?
  • 请分享您告诉警报对话框使用该样式的代码。尝试遵循本指南:stackoverflow.com/questions/2422562/…
  • @David 已编辑代码。请检查。
  • @BrunoFerreira 我有 v21/styles 但没有 v23/styles。
  • 尝试添加具有相同警报对话框样式的 v23/styles 并查看是否有效

标签: android android-alertdialog android-styles


【解决方案1】:

您必须从代码中执行 if。如下:

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this, R.style.YourAlertDialogStyle);
    alertDialogBuilder.setTitle("title");
    alertDialogBuilder.setMessage("message");
    alertDialogBuilder.setCancelable(false);
    final AlertDialog alertDialog = alertDialogBuilder.create();
    //2. now setup to change color of the button
    alertDialog.setOnShowListener(
            new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface arg0) {
                // like this set text color to all your buttons in dialog
                alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(COLOR_YOU_WANT);
            }
    });

编码愉快!!!

【讨论】:

    猜你喜欢
    • 2021-08-08
    • 2020-03-06
    • 1970-01-01
    • 2013-07-25
    • 2014-12-23
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 2019-02-10
    相关资源
    最近更新 更多