[英]Android - How to programmatically set the color of the icon of an AlertDialog?
I want to use the same icon (which is a vector
) with various colors by programmatically setting its color as we do thanks to the android:tint
property. 我想通过编程方式设置其颜色来使用相同的图标( vector
),这要归功于android:tint
属性。
Here is the builder
that sets the icon of AlertDialog
: 这是设置AlertDialog
图标的builder
:
builder.setIcon(android.R.drawable.ic_dialog_info);
Is there a way to do that? 有没有办法做到这一点? Do we have to create the same icon again if we just want to use it with a different color? 如果我们只想使用其他颜色,是否需要再次创建相同的图标?
You can get the ImageView
and set the ColorFilter
. 您可以获取ImageView
并设置ColorFilter
。 try the code below. 尝试下面的代码。 See if it works as expected. 查看它是否按预期工作。
AlertDialog.Builder builder = new AlertDialog.Builder(DemoActivity.this);
builder.setMessage("Message");
builder.setTitle("Title");
builder.setIcon(android.R.drawable.ic_dialog_info);
AlertDialog dialog = builder.create();
dialog.show();
ImageView imageView = dialog.findViewById(android.R.id.icon);
if (imageView != null)
imageView.setColorFilter(Color.BLUE, android.graphics.PorterDuff.Mode.SRC_IN);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.