setStrokeColor无法以编程方式工作

问题描述 投票:0回答:2

我正在尝试为按钮的轮廓设置颜色,但我无法使其正常工作

我正在使用材质按钮,并且在使用时

  button.setStrokeColorResource(Color.parseColor(#e4dcd4))

不起作用,并告诉我这一点

期望颜色资源ID(R.color。),但收到RGB整数

我尝试了几乎可以在堆栈中找到的所有内容,但是无法以编程方式设置它的笔触颜色

编辑

几乎所有setColors使用@ColorInt,但是此strokeColor使用@ColorRes,这对我不起作用,还有setStrokeColor

public void setStrokeColor(@Nullable ColorStateList strokeColor) {
    if (isUsingOriginalBackground()) {
      materialButtonHelper.setStrokeColor(strokeColor);
    }
  }

但是我也无法使它工作

android android-studio kotlin android-button
2个回答
1
投票

您可以尝试这个

button.setStrokeColor(ContextCompat.getColor(this, R.color.your_color_xml));

其他可以做的是

ShapeDrawable gradientDrawable = (ShapeDrawable)button.getBackground(); 
gradientDrawable.setStroke(2, your_color); 

也像@Gabriele所说的那样,你可以得到一个int作为颜色:

//From RGB
int colorRGB = Color.rgb(255,0,0);

//From HEX String
int colorHEX = Color.parseColor("#FF11AA");

0
投票

它像这样工作

val colorInt = Color.parseColor("#e4dcd4")
            val csl = ColorStateList.valueOf(colorInt)
            my_button.strokeColor = csl
© www.soinside.com 2019 - 2024. All rights reserved.