以编程方式更改 Android AlertDialog 中单个项目的背景颜色

Programmatically change individual item background colour in Android AlertDialog

我试图在我的 Android 应用程序中提供特定颜色的快速选择,我希望能够在一个简单的对话框中将列表选项设置为特定颜色。看起来应该很容易,但我一直无法找到直接的答案。

如何(在 运行 时以编程方式)相应地设置每个选项的背景颜色?

我认为这可能有用:

private void showAlertDialogForColors() {
        final AlertDialog.Builder alert = new AlertDialog.Builder(this);
        LayoutInflater inflater = this.getLayoutInflater();
        View alertLayout = inflater.inflate(R.layout.choose_color_theme, null);
        //alert.setTitle("Theme Color");
        // this is set the view from XML inside AlertDialog
        alert.setView(alertLayout);
        final Integer tempValue = SessionManager.getInstance(ColorSettingsActivity.this).getPrefThemeCode();

        TextView textViewBlue = (TextView) alertLayout.findViewById(R.id.themeColorBlue);
        TextView textViewOrange = (TextView) alertLayout.findViewById(R.id.themeColorOrange);
        TextView textViewPink = (TextView) alertLayout.findViewById(R.id.themeColorPink);
        TextView textViewPurple = (TextView) alertLayout.findViewById(R.id.themeColorPurple);
        TextView textViewCyan = (TextView) alertLayout.findViewById(R.id.themeColorCyan);
        TextView textViewDarkGreen = (TextView) alertLayout.findViewById(R.id.themeColorDarkGreen);
        selectedThemeColor = (TextView) alertLayout.findViewById(R.id.textViewSelectedTheme);
        selectedThemeColor.setTypeface(font);

        textViewBlue.setOnClickListener(this);
        textViewOrange.setOnClickListener(this);
        textViewPink.setOnClickListener(this);
        textViewPurple.setOnClickListener(this);
        textViewCyan.setOnClickListener(this);
        textViewDarkGreen.setOnClickListener(this);


        // disallow cancel of AlertDialog on click of back button and outside touch
        alert.setCancelable(false);
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                SessionManager.getInstance(MessageSettingsActivity.this).setPrefThemeCode(tempValue);
                Toast.makeText(getBaseContext(), "Cancel clicked", Toast.LENGTH_SHORT).show();
            }
        });
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                recreate();
            }
        });
        AlertDialog dialog = alert.create();
        dialog.show();
    }

如果您有一组预定义的颜色,请使用此选项,或者您想传递动态存储在 Shared PreferencesDB 中的颜色并填充到列表中

然后在他点击特定项目时实施开关来执行您的操作,在我的例子中有 5 个常量。

谢谢大家的回复。我想通了...

private void showColourChooserDialog(){
    final ColourItem[] items = {
            new ColourItem(Color.RED),
            new ColourItem(Color.GREEN),
            new ColourItem(Color.BLUE),
    };      

    ListAdapter adapter = new ArrayAdapter<ColourItem>(
            this,
            android.R.layout.select_dialog_item,
            android.R.id.text1,
            items){
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);
            TextView tv = (TextView)v.findViewById(android.R.id.text1);

            int colour = items[position].getColour();
            tv.setBackgroundColor(colour);

            return v;
        }
    };

    new AlertDialog.Builder(this)
    .setTitle("Choose Colour")
    .setAdapter(adapter, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // ... respond to choice here
        }
    }).show();
}

ColourItem class:

public class ColourItem {
    private String displayString;
    private int colour;

    public ColourItem (int colour) {
        this(colour, "");
    }
    public ColourItem (int colour, String displayString) {
        this.colour = colour;
        this.displayString = displayString;
    }

    @Override
    public String toString() {
        return displayString;
    }
    public void setDisplayString(String s){
        this.displayString = s;
    }

    public int getColour(){
        return colour;
    }
    public void setColour(int i){
        this.colour = i;
    }
}

回答灵感来自:This solution regarding custom icons for dialog items