【问题标题】:Add Color "#e3bb87" to StateListDrawable programmatically以编程方式将颜色“#e3bb87”添加到 StateListDrawable
【发布时间】:2013-03-01 05:49:23
【问题描述】:

我需要以编程方式这样做的原因是文本颜色是下载的,而不是在 xml 中预先定义的。我读了这个 Replace selector images programmatically

我只需要知道

StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},
    getResources().getDrawable(R.drawable.pressed));

怎么变成

states.addState(new int[] {android.R.attr.state_pressed},**theMethodImLookingFor**("#e3bb87"));

忘记getResources().getColor(R.color.anycolor),xml中没有定义颜色

【问题讨论】:

  • 你可以尝试使用 text.setTextColor(0xff00ff00);
  • 这是一个有趣的观点。我建议从标题中删除明确的“#e3bb87”,以便人们可以轻松找到任何颜色。

标签: android textview android-drawable android-selector


【解决方案1】:

你可以用这个:

states.addState(new int[] {android.R.attr.state_pressed},
    new ColorDrawable(Color.parseColor("#e3bb87")));

【讨论】:

    【解决方案2】:

    我想你在找ColorDrawable

    你可以这样做:

    StateListDrawable states = new StateListDrawable();
    int color = 0xff00ff00;
    states.addState(new int[] {android.R.attr.state_pressed},
         new ColorDrawable(color)); 
    

    【讨论】:

    • 谢谢,我只能接受一个答案。像汉兰达这样的“只能有一个”
    【解决方案3】:

    方法是

    new ColorDrawable(Color.parseColor("#e3bb87"))
    

    【讨论】:

      猜你喜欢
      • 2011-07-20
      • 2013-08-13
      • 2017-05-11
      • 1970-01-01
      • 2021-12-18
      • 2020-02-19
      • 2011-09-24
      • 2013-06-12
      • 1970-01-01
      相关资源
      最近更新 更多