如何获取按钮的背景颜色。 在 XML 中,我使用 ---- android:background = XXXXX 来设置背景颜色。 现在在活动类中,我该如何检索它所具有的值?
注:XXXXX 是指在 XML 中设置的实际背景颜色值。如何获取按钮的背景颜色。 在 XML 中,我使用 ---- android:background = XXXXX 来设置背景颜色。 现在在活动类中,我该如何检索它所具有的值?
注:XXXXX 是指在 XML 中设置的实际背景颜色值。很遗憾,我不知道如何获取实际颜色。
将其作为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");
}
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 salimirow.getBackground()
是否确实是ColorDrawable
,则不应将其转换为ColorDrawable
。这就是为什么我要从if(view.getBackground() instanceof ColorDrawable)
开始的原因。在StateListDrawable
(一个DrawableContainer
)的情况下,您可能需要使用drawable.getCurrent()
进行操作。然而,仅凭这一点可能不足够。这真的取决于可绘制对象的结构。此外,请注意,这个片段已经有近7年的历史了,很多东西已经发生了变化。祝你好运。 - jpmcostaandroid:tag="#ff0000"
并且可以从代码中访问它
String colorCode = (String)btn.getTag();
对我来说,获取颜色的最简单方法是:
int color = ((ColorDrawable)button.getBackground()).getColor();
在Lollipop 5.1.1上经过测试并可正常工作。
Drawable
,您可以使用:public Drawable getBackground();
View
类中定义的一部分。Button
可以具有图像、颜色或渐变的背景。如果使用android:background="#ffffff",则背景的类将是
从那里,你只需调用android.graphics.drawable.ColorDrawable
public int getColor()
试试这个:
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();
int colornumber=((ColorDrawable)v.getBackground()).getColor();
这是获取视图(按钮、文本视图等)颜色的最佳简单方法
使用Java设置视图颜色,我们使用
v.setBackgroundColor(getColor(R.color.colorname_you_used));
button.setBackground(R.color.green)
然后检查响应,但它肯定不是实际的颜色ID。我更喜欢一个正确的颜色整数,这样我就可以使用Color.red(int)
、Color.green(int)
、Color.blue(int)
来处理它。但在我的 Android 3.2 测试中,情况并非如此。我猜这可能是不一致的,具体取决于上下文,返回一个颜色整数或 resid。 - Matthew Rudy((ColorDrawable) row.getBackground()).getColor()
,想改成(row.background as ColorDrawable).color
,但是遇到了错误android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.ColorDrawable
。 - milad salimi