如何更改警报对话框消息的颜色?

2022-09-01 16:40:27

我有一个,它的消息被显示,但文本的颜色是白色的。它与背景融为一体。我尝试过更改主题,但它不起作用。如何更改邮件的颜色?AlertDialog

相关代码:

AlertDialog.Builder builder;
builder = new AlertDialog.Builder(MainActivityGame.this);

builder.setTitle("Name");
builder.setMessage("Are you ");
builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        //Submit default name, go home
        boolean isInserted = myDb.insertData(defaultName, triesTaken, difficultyText);
        if (isInserted) {
            Toast.makeText(MainActivityGame.this, "Your name was submitted", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(MainActivityGame.this, "Error, your name wasn't submitted\n Have you entered a default name?\n Go to Settings/Default Name to set it up", Toast.LENGTH_SHORT).show();
        }
        Intent intent = new Intent(MainActivityGame.this, MainActivity.class);
        startActivity(intent);
    }
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        userName.setVisibility(View.VISIBLE);
        submitName.setVisibility(View.VISIBLE);
        submitName.setEnabled(true);
        dialogInterface.dismiss();
    }
});
builder.create();
builder.show();

答案 1

您可以为警报对话框指定样式,如下所示:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AlertDialogStyle);

风格一如既往:

<style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog">
    <item name="android:colorAccent">#f3f3f3</item>
    <item name="android:textColor">#f3f3f3</item>
    <item name="android:textColorPrimary">#f3f3f3</item>
</style>

答案 2

您可以使用此方法(如@KarimElGhandour所述方法),也可以直接在文件夹中创建自定义,然后使用 应用它。Layoutres\layoutalertDialog.setView(LayoutInflater.inflate(R.layout.yourlayout), yourRootView


推荐