【发布时间】:2013-01-06 15:20:07
【问题描述】:
我想以编程方式在 FrameLayout 中设置前景色(不是在 XML 属性中)。我有RGB中的颜色代码
如何将颜色转换为可绘制对象:
frm.setForeground(Drawable);
【问题讨论】:
标签: android colors foreground
我想以编程方式在 FrameLayout 中设置前景色(不是在 XML 属性中)。我有RGB中的颜色代码
如何将颜色转换为可绘制对象:
frm.setForeground(Drawable);
【问题讨论】:
标签: android colors foreground
您可以从颜色创建Drawable:
final int color = 0xFFFF0000;
final Drawable drawable = new ColorDrawable(color);
【讨论】:
new ColorDrawable( context.getResources().getColor( color))。
使用ContextCompat从颜色创建Drawable
int color = R.color.black_trans_60;
frm.setForeground(new ColorDrawable(ContextCompat.getColor(mContext, color)));
使用 ContextCompat 而不是直接颜色,因为新 API ColorDrawable 采用 ColorDrawable(@ColorInt int color)
【讨论】: