【发布时间】:2011-12-26 16:56:23
【问题描述】:
如何获取按钮的背景颜色。 在 xml 中,我使用 ---- android:background = XXXXX 设置背景颜色 现在在活动类中我如何检索它具有的这个值?
【问题讨论】:
标签: android button background-color
如何获取按钮的背景颜色。 在 xml 中,我使用 ---- android:background = XXXXX 设置背景颜色 现在在活动类中我如何检索它具有的这个值?
【问题讨论】:
标签: android button background-color
很遗憾,我不知道如何检索实际颜色。
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");
}
【讨论】:
button.setBackground(R.color.green) 然后检查了响应,它肯定不是实际的颜色ID。我更喜欢一个合适的颜色整数,所以我可以Color.red(int)、Color.green(int)、Color.blue(int) 它。但在我对 Android 3.2 的测试中,情况并非如此。我猜这是不一致的,根据上下文返回颜色 int 或残差。
((ColorDrawable) row.getBackground()).getColor() 作为(row.background as ColorDrawable).color 但我遇到了这个错误android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.ColorDrawable
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
row.getBackground() 转换为ColorDrawable 而不先检查它是否确实是ColorDrawable。这就是我从if(view.getBackground() instanceof ColorDrawable) 开始的原因。对于StateListDrawable(DrawableContainer),您可能需要使用drawable.getCurrent()。然而,仅此可能还不够。这实际上取决于drawable的结构。另外,请注意,这个 sn-p 已经有将近 7 年了,从那时起发生了很多变化。祝你好运。
您也可以尝试将颜色值设置为标签,例如
android: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));
【讨论】: