更改 Android 中的分隔线颜色

Change divider color in Android

我正在尝试自定义我的警报对话框,但我似乎无法找到一种方法来更改文本消息和按钮之间的分隔符。

我的 styles.xml:

中有这个自定义警报对话框主题
<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/maroon</item>
    <item name="colorAccent">@color/primary</item>
</style>

这是我 Activity 中的警报对话框:

final AlertDialog.Builder alertDialog = new AlertDialog.Builder(ChefMenuActivity.this,R.style.AlertDialogCustom);



                    final EditText edittext = new EditText(getApplicationContext());
                    alertDialog.setMessage("Item name: " + menuList.get(position).getItemName() + "\n" + "Old quantity: " + menuList.get(position).getQty_left());
                    alertDialog.setTitle("Change item quantity");

                    edittext.setTextColor(Color.BLACK);
                    edittext.setHint("E.g.: 10");
                    edittext.setHintTextColor(Color.GRAY);
                    edittext.setInputType(InputType.TYPE_CLASS_NUMBER);

                    alertDialog.setView(edittext);

                    alertDialog.setPositiveButton("REMOVE", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            Toast.makeText(getApplicationContext(), "Not impemented yet", Toast.LENGTH_SHORT).show();
                            dialog.cancel();
                        }
                    });

                    alertDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            Toast.makeText(getApplicationContext(), "Not impemented yet", Toast.LENGTH_SHORT).show();
                            dialog.cancel();
                        }
                    });

                    alertDialog.setNeutralButton("UPDATE", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            Toast.makeText(getApplicationContext(), "Not impemented yet", Toast.LENGTH_SHORT).show();
                            dialog.cancel();
                        }
                    });

                    alertDialog.show();

我找到了一段代码来更改标题和消息之间的分隔符:

                AlertDialog dialog = alertDialog.show();

                int titleDividerId = getResources().getIdentifier("titleDivider", "id", "android");
                View titleDivider = dialog.findViewById(titleDividerId);
                if (titleDivider != null)
                    titleDivider.setBackgroundColor(Color.parseColor("#ff4444"));

但这对我的情况无效,因为我想更改消息和按钮之间的分隔符(如下图所示)。

使用这个库

https://github.com/danoz73/QustomDialog

QustomDialogBuilder qustomDialogBuilder = new QustomDialogBuilder(context).
    setTitle("Set IP Address").
    setTitleColor("#FF00FF").
    setDividerColor("#FF00FF").
    setMessage("You are now entering the 10th dimension.").

qustomDialogBuilder.show();

或者你可以这样做

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.dialog)
       .setIcon(R.drawable.ic)
       .setMessage(R.string.dialog_msg);
   Dialog d = builder.show();
int dividerId = d.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
View divider = d.findViewById(dividerId);
divider.setBackgroundColor(getResources().getColor(R.color.my_color));

您可以找到更多答案here

正如上面所说,我想改变的不是分隔线,而是编辑文本的背景。我不得不使用 editText.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN);更改编辑文本下划线颜色。