Android: Create a ColorDrawable object without using xml
Is it possible to create a ColorDrawable object without using开发者_运维问答 xml? I would like to be able to change the backgroundColor of a view programmatically, using setBackgroundColor() or setBackgroundDrawable() or setBackgroundResource(), but I want to be able to specify the RGB values in code, not XML. Is this possible?
I know you can get a View as a Drawable and apply a color filter to it (useful for coloring in Button views) by doing the following:
Drawable d_delete = findViewById(R.id.btn_delete).getBackground();
PorterDuffColorFilter filter_red = new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
d_delete.setColorFilter(filter_red);
I know the questions it's a bit old but I've got an easy one. Maybe someone arrive here looking for the answer
View view = findViewById(R.id.view_with_colored_background);
String rgbColor = "#CCFFCC";
view.setBackgroundColor(Color.parseColor(rgbColor));
精彩评论