在安卓中获取按钮的背景颜色

45

如何获取按钮的背景颜色。 在 XML 中,我使用 ---- android:background = XXXXX 来设置背景颜色。 现在在活动类中,我该如何检索它所具有的值?

注:XXXXX 是指在 XML 中设置的实际背景颜色值。
7个回答

87

很遗憾,我不知道如何获取实际颜色。

将其作为Drawable获取很容易。

Button button = (Button) findViewById(R.id.my_button);
Drawable buttonBackground = button.getBackground();

如果你知道这是一种颜色,那么你可以尝试

ColorDrawable buttonColor = (ColorDrawable) button.getBackground();

如果您使用的是 Android 3.0+,您可以获取颜色资源的 ID。

int colorId = buttonColor.getColor();

将其与您分配的颜色进行比较,即:

if (colorID == R.color.green) {
  log("color is green");
}

2
你确定getColor()获取id吗?我认为它获取颜色的实际int值(即0xAARRGGBB)。我用“#00000001”进行了测试,它返回1。 - brianestey
1
在我的测试中,我执行了 button.setBackground(R.color.green) 然后检查响应,但它肯定不是实际的颜色ID。我更喜欢一个正确的颜色整数,这样我就可以使用 Color.red(int)Color.green(int)Color.blue(int) 来处理它。但在我的 Android 3.2 测试中,情况并非如此。我猜这可能是不一致的,具体取决于上下文,返回一个颜色整数或 resid。 - Matthew Rudy
嘿,我正在尝试仅在图像按钮的背景图像是特定可绘制资源时执行任务。我该如何比较...我已经尝试了if(buttonBackground.equals(@drawable/mydrawable)),但它不起作用。 - Ruchir Baronia
3
我用这个代码 ((ColorDrawable) row.getBackground()).getColor() ,想改成 (row.background as ColorDrawable).color ,但是遇到了错误 android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.ColorDrawable - milad salimi

19
private Bitmap mBitmap;
private Canvas mCanvas;
private Rect mBounds;

public void initIfNeeded() {
  if(mBitmap == null) {
    mBitmap = Bitmap.createBitmap(1,1, Bitmap.Config.ARGB_8888);
    mCanvas = new Canvas(mBitmap);
    mBounds = new Rect();
  }
}

public int getBackgroundColor(View view) {
  // The actual color, not the id.
  int color = Color.BLACK;

  if(view.getBackground() instanceof ColorDrawable) {
    if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
      initIfNeeded();

      // If the ColorDrawable makes use of its bounds in the draw method,
      // we may not be able to get the color we want. This is not the usual
      // case before Ice Cream Sandwich (4.0.1 r1).
      // Yet, we change the bounds temporarily, just to be sure that we are
      // successful.
      ColorDrawable colorDrawable = (ColorDrawable)view.getBackground();

      mBounds.set(colorDrawable.getBounds()); // Save the original bounds.
      colorDrawable.setBounds(0, 0, 1, 1); // Change the bounds.

      colorDrawable.draw(mCanvas);
      color = mBitmap.getPixel(0, 0);

      colorDrawable.setBounds(mBounds); // Restore the original bounds.
    }
    else {
      color = ((ColorDrawable)view.getBackground()).getColor();
    }
  }

  return color;
}

我曾使用过 ((ColorDrawable) row.getBackground()).getColor() ,将其转换为 (row.background as ColorDrawable).color,但是我遇到了这个错误 android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.ColorDrawable - milad salimi
如果没有先检查row.getBackground()是否确实是ColorDrawable,则不应将其转换为ColorDrawable。这就是为什么我要从if(view.getBackground() instanceof ColorDrawable)开始的原因。在StateListDrawable(一个DrawableContainer)的情况下,您可能需要使用drawable.getCurrent()进行操作。然而,仅凭这一点可能不足够。这真的取决于可绘制对象的结构。此外,请注意,这个片段已经有近7年的历史了,很多东西已经发生了变化。祝你好运。 - jpmcosta

17
你可以尝试像这样将颜色值设置为标签:
android:tag="#ff0000"

并且可以从代码中访问它

String colorCode = (String)btn.getTag();

我认为这是最好的方法,也能正常工作:@color/your_color 或者 ?attr/colorPrimary。 - Noor Hossain
一切伟大的创意都很简单!谢谢! - user_MGU

10

对我来说,获取颜色的最简单方法是:

int color = ((ColorDrawable)button.getBackground()).getColor();

在Lollipop 5.1.1上经过测试并可正常工作。


2
为了获得背景的Drawable,您可以使用:
public Drawable getBackground();

作为基础的View类中定义的一部分。
不要忘记Button可以具有图像、颜色或渐变的背景。如果使用android:background="#ffffff",则背景的类将是

android.graphics.drawable.ColorDrawable

从那里,你只需调用
public int getColor()

0

试试这个:

list_view.getChildAt(position).setBackgroundColor(Color.YELLOW);        
ColorDrawable corItem = (ColorDrawable) list_view.getChildAt(position).getBackground();
if(corItem.getColor() == Color.YELLOW){
   Toast.makeText(NovoProcessoActivity.this,"Right Color!", Toast.LENGTH_SHORT).show();
   }else{
   Toast.makeText(NovoProcessoActivity.this,"Wrong Color!", Toast.LENGTH_SHORT).show();
}

或者

int color =( (ColorDrawable)  list_view.getChildAt(position).getBackground()).getColor();

0
int colornumber=((ColorDrawable)v.getBackground()).getColor();

这是获取视图(按钮、文本视图等)颜色的最佳简单方法

使用Java设置视图颜色,我们使用

v.setBackgroundColor(getColor(R.color.colorname_you_used));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接