我需要创建一个Drawable具有所需颜色作为背景颜色的简单对象,但我不知道如何在不使用XML模式的情况下以编程方式执行此操作(可能真的吗?).请告诉我,我需要它来制作LayerDrawable和使用SeekBar(以SeekBar编程方式更改背景).先感谢您.
在您的情况下,ColorDrawable会对您有所帮助,您可以为您的drawable 传递参数颜色.
或者您可以执行以下操作:
ImageView imgStatus = (ImageView) findViewById(R.id.imgInfoIcon);
// Load the icon as drawable object
Drawable d = getResources().getDrawable(R.drawable.ic_menu_info_details);
// Get the color of the icon depending on system state
int iconColor = android.graphics.Color.BLACK
if (systemState == Status.ERROR)
iconColor = android.graphics.Color.RED
else if (systemState == Status.WARNING)
iconColor = android.graphics.Color.YELLOW
else if (systemState == Status.OK)
iconColor = android.graphics.Color.GREEN
// Set the correct new color
d.setColorFilter( iconColor, Mode.MULTIPLY );
// Load the updated drawable to the image viewer
imgStatus.setImageDrawable(d);
Run Code Online (Sandbox Code Playgroud)
上面的代码最初发布在这里
正如@Thrakbad建议的那样,您可以使用ColorDrawable:
TextView textView = new TextView(this);
ColorDrawable colorDrawable = new ColorDrawable(0xFFFF0000);
textView.setBackgroundDrawable(colorDrawable);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21531 次 |
| 最近记录: |