如何更改AlertDialog消息的颜色?

时间:2017-06-18 19:09:36

标签: java android android-alertdialog

我有一个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();

4 个答案:

答案 0 :(得分:27)

您可以为您的提醒对话框添加样式:

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>

答案 1 :(得分:0)

您可以使用this方法,例如@KarimElGhandour,也可以在Layout文件夹中创建自定义res\layout,然后将其应用于alertDialog.setView(LayoutInflater.inflate(R.layout.yourlayout), yourRootView

答案 2 :(得分:0)

在样式中,您甚至可以更改重力模式

<style name="WelcomeStyle" parent="Theme.AppCompat.Light.Dialog">
        <item name="android:background">#FFD600</item>
        <item name="android:textColor">#C51162</item>
        <item name="android:textColorPrimary">#00B8D4</item>
        <item name="android:gravity">center</item>
        <item name="android:justificationMode">inter_word</item>
    </style>

答案 3 :(得分:0)

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

styles.xml

  <style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog">
    <item name="android:colorAccent">#303030</item>
    <item name="android:textColor">#FFFFFF</item>
    <item name="android:textColorPrimary">#494949</item>
    <item name="android:windowBackground">#201D1D</item>
</style>