Java 如何更改 AlertDialog 视图背景颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20742846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:00:42  来源:igfitidea点击:

How to change AlertDialog view background color?

javaandroidandroid-layoutandroid-alertdialog

提问by sider

I want to know how could I change that grey color on my alert dialog? I have tried that:

我想知道如何更改警报对话框中的灰色?我试过了:

layout.setBackgroundResource(R.color.Aqua);

It didn't work. Any ideas?

它没有用。有任何想法吗?

My AlertDialog

我的警报对话框

I have created AlertDialog with the following code:

我使用以下代码创建了 AlertDialog:

public class CustomInputDialog{

    private OnDialogClickListener listener;
    private Context context;
    private String title;
    private String message;
    EditText input;
    LinearLayout layout;

    public interface OnDialogClickListener {
        void onDialogOKClick(String value);
    }

    public CustomInputDialog(String title, String message, Context context, OnDialogClickListener listener) {

        super();
        this.title = title;
        this.message = message;
        this.context = context;
        this.listener = listener;

        layout = new LinearLayout(context);
        layout.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(20, 10, 20, 10);

        input = new EditText(context);

        InputFilter[] filters = new InputFilter[1];
        filters[0] = new InputFilter.LengthFilter(20);
        input.setFilters(filters);

        layout.addView(input, params);

    }

    private void dialog(){

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setCancelable(true);
        builder.setView(layout);
        builder.setTitle(title);
        builder.setMessage(message); 
        builder.setInverseBackgroundForced(true);
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
                String value = input.getText().toString();
                listener.onDialogOKClick(value);
                dialog.dismiss();
          }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
              }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }

}

采纳答案by Sanket Kachhela

you can set custom view programmatically like this way..

您可以像这样以编程方式设置自定义视图..

LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.dialog_layout, (ViewGroup) getCurrentFocus());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialoglayout);
builder.show();

then after to get reference of component

然后在获取组件的引用之后

e.g. Button btn = (Button) dialoglayout.findViewById(R.id.button_id);

回答by Nambi

you have to create custom dialogto make changes in background color refer this link

您必须创建custom dialog以更改背景颜色,请参阅此链接

回答by Makvin

<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:background">@color/myColor</item>
</style>

AlertDialog.Builder builder = new AlertDialog.Builder(Datetimeactivity.this,R.style.AlertDialogTheme);