如何更改PreferenceFragment Alert对话框的分隔颜色

时间:2015-10-01 16:12:14

标签: android alertdialog android-styles

我无法弄清楚如何更改选择首选项时弹出的警报对话框的分隔颜色。我知道可以通过实现扩展AlertDialog的自定义类并在show()方法中执行以下操作:

int dividerId = getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
View divider = dialog.findViewById(dividerId);
divider.setBackgroundColor(themeColor);

但是,不知道如何强制PreferenceFragment中出现的警告对话框来扩展我的自定义AlertDialog。

我也可以使用样式来修改PreferenceFragment中AlertDialog的外观,但是没有与AlertDialogs的分隔颜色对应的样式属性(因此必须实现查找分隔符视图的黑客)。

有没有人知道如何在不实现自己的PreferenceFragment的情况下实现这一目标?

我的应用程序的基本主题是Theme.AppCompat。

1 个答案:

答案 0 :(得分:2)

这完全是为了改变AlertDialog中的颜色:

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Test Title");
        builder.setMessage(Html.fromHtml("<font color='#FF7F27'>This is a test</font>"));
        builder.setCancelable(false);
        builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
        try {
            Resources resources = dialog.getContext().getResources();
            int alertTitleId = resources.getIdentifier("alertTitle", "id", "android");
            TextView alertTitle = (TextView) dialog.getWindow().getDecorView().findViewById(alertTitleId);
            alertTitle.setTextColor(Color.MAGENTA); // change title text color

            int titleDividerId = resources.getIdentifier("titleDivider", "id", "android");
            View titleDivider = dialog.getWindow().getDecorView().findViewById(titleDividerId);
            titleDivider.setBackgroundColor(Color.YELLOW); // change divider color
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        Button nbutton = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
        //Set negative button background
        nbutton.setBackgroundColor(Color.MAGENTA);
        //Set negative button text color
        nbutton.setTextColor(Color.YELLOW);
        Button pbutton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
        //Set positive button background
        pbutton.setBackgroundColor(Color.YELLOW);
        //Set positive button text color
        pbutton.setTextColor(Color.MAGENTA);

这是我的示例代码,但是如果要更改分隔符颜色,请考虑代码的一部分以“int titleDividerId”开头。我希望它有所帮助。

结果:

This is the result of the code

相关问题