Android AlertDialog setMultiChoiceItems具有自定义背景色 [英] Android AlertDialog setMultiChoiceItems with custom background color

查看:237
本文介绍了Android AlertDialog setMultiChoiceItems具有自定义背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到了带有多个选择项选择的AlertDialog:

I got this alertDialog with a multiple choice item selection:

builder.setTitle(R.string.layer_options_title)
    .setMultiChoiceItems(availableOptions, selectedLayerOptions, new DialogInterface.OnMultiChoiceClickListener() {
        ...
    });

其中

availableOptions = getApplicationContext().getResources().getStringArray(R.array.layer_options);

来自

<string-array name="layer_options">
    <item>Should have green background</item>
    <item>Should have yellow background</item>
    <item>Should have orange background</item>
    <item>Should have blue background</item>
</string-array>

有什么方法可以使这些选择项具有背景颜色?

Is there any way to make these multiple choice items have a background color?

推荐答案

是的,有一种方法.将您的复选框放在布局中,并为布局提供背景色.这是我的方法:

Yes, there is a way. Put your checkbox in a layout and give the layout backgrond color. This is how I could do it:

AlertDialog.Builder alert = new AlertDialog.Builder(this);
        LinearLayout mainLayout       = new LinearLayout(this);
        mainLayout.setOrientation(LinearLayout.VERTICAL);

        LinearLayout layout1       = new LinearLayout(this);
        layout1.setOrientation(LinearLayout.HORIZONTAL);
        CheckBox cb1 = new CheckBox(getApplicationContext());
        cb1.setText("Easy");
        layout1.addView(cb1);
        layout1.setBackgroundColor(Color.BLUE);
        layout1.setMinimumHeight(50);

        LinearLayout layout2       = new LinearLayout(this);
        layout2.setOrientation(LinearLayout.HORIZONTAL);
        layout2.addView(new TextView(this));
        CheckBox cb2 = new CheckBox(getApplicationContext());
        cb2.setText("Normal");
        layout2.addView(cb2);
        layout2.setBackgroundColor(Color.CYAN);
        layout2.setMinimumHeight(50);

        LinearLayout layout3       = new LinearLayout(this);
        layout3.setOrientation(LinearLayout.HORIZONTAL);
        CheckBox cb3 = new CheckBox(getApplicationContext());
        cb3.setText("Hard");
        layout3.addView(cb3);
        layout3.setBackgroundColor(Color.GREEN);
        layout3.setMinimumHeight(50);

        mainLayout.addView(layout1);
        mainLayout.addView(layout2);
        mainLayout.addView(layout3);
        alert.setTitle("Custom alert demo");
        alert.setView(mainLayout);
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        alert.setPositiveButton("Done", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getBaseContext(), "done", Toast.LENGTH_SHORT).show();
            }
        });

        alert.show();

结果:

首先,如您在代码中看到的,我创建了一个主布局(垂直).然后,对于每个复选框,我创建了一个水平布局.在这种情况下,您可以使用元素的颜色和字体(复选框,项目等).

Firstly, I created a main layout (vertical) as you see in the code. Then, for each one of the checkboxes I created a horizontal layout. In this case you can play with the colors and fonts of the elements (checkboxes, items, and etc.).

这篇关于Android AlertDialog setMultiChoiceItems具有自定义背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆