【问题标题】:Set foreground color in FrameLayout in Android programmatically以编程方式在Android的FrameLayout中设置前景色
【发布时间】:2013-01-06 15:20:07
【问题描述】:

我想以编程方式在 FrameLayout 中设置前景色(不是在 XML 属性中)。我有RGB中的颜色代码

如何将颜色转换为可绘制对象:

frm.setForeground(Drawable);

【问题讨论】:

    标签: android colors foreground


    【解决方案1】:

    您可以从颜色创建Drawable

    final int color = 0xFFFF0000;
    final Drawable drawable = new ColorDrawable(color);
    

    【讨论】:

    • 仅用于透明 .setForeground(null);
    • 来自资源的颜色为new ColorDrawable( context.getResources().getColor( color))
    【解决方案2】:

    使用ContextCompat从颜色创建Drawable

    int color = R.color.black_trans_60;
    frm.setForeground(new ColorDrawable(ContextCompat.getColor(mContext, color)));
    

    使用 ContextCompat 而不是直接颜色,因为新 API ColorDrawable 采用 ColorDrawable(@ColorInt int color)

    【讨论】:

      猜你喜欢
      • 2012-09-25
      • 2016-12-28
      • 2017-10-30
      • 1970-01-01
      • 2011-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-18
      相关资源
      最近更新 更多