2011-11-11 40 views
33

Làm cách nào để tôi nhận được màu nền của một nút. Trong xml tôi thiết lập màu nền bằng cách sử dụng ---- android: background = XXXXX bây giờ trong lớp hoạt động như thế nào tôi có thể lấy giá trị này mà nó có?Lấy màu nền của một nút trong android

Trả lời

73

Thật không may là tôi không biết cách lấy màu thực tế.

Thật dễ dàng để có được điều này như một Drawable

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

Nếu bạn biết đây là một màu sau đó bạn có thể thử

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

Và nếu bạn đang sử dụng Android 3.0+ bạn có thể nhận ra id tài nguyên của màu.

int colorId = buttonColor.getColor(); 

Và so sánh điều này với màu được chỉ định của bạn, ví dụ:

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

Bạn có chắc chắn getColor() được id? Tôi nghĩ rằng nó nhận được giá trị int thực tế của màu (ví dụ: 0xAARRGGBB). Tôi đã thử nghiệm điều này với "# 00000001" và nó trả lại 1. – brianestey

+1

Trong thử nghiệm của tôi, tôi đã làm 'button.setBackground (R.color.green)' và sau đó kiểm tra phản hồi và chắc chắn nó không phải là id màu thực. Tôi muốn một số nguyên thích hợp màu sắc vì vậy tôi có thể 'Color.red (int)', 'Color.green (int)', 'Color.blue (int)' nó. Nhưng trong thử nghiệm của tôi trên Android 3.2, đây không phải là trường hợp.Tôi đoán rằng nó không nhất quán, trả về một màu int, hoặc dư, tùy thuộc vào ngữ cảnh. –

+1

chỉ là API 11+ –

2

Để có được nền Drawable, bạn sử dụng

public Drawable getBackground(); 

theo quy định tại các cơ sở View lớp.

Đừng quên rằng Button có thể có nền là hình ảnh, màu, độ dốc. Nếu bạn sử dụng android: background = "# ffffff", lớp nền sẽ là

android.graphics.drawable.ColorDrawable

Từ đó bạn chỉ có thể gọi

public int getColor() 
14

Bạn cũng có thể thử một cái gì đó giống như thiết lập giá trị màu như thẻ như

android:tag="#ff0000" 

Và truy cập nó từ mã

String colorCode = (String)btn.getTag(); 
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; 
} 
+3

này phải là câu trả lời được chấp nhận – IHeartAndroid

5

Cách đơn giản nhất để có được màu cho tôi là:

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

Tested và làm việc trên Lollipop 5.1.1

0

Hãy thử điều này:

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(); 
} 

hoặc

int color =((ColorDrawable) list_view.getChildAt(position).getBackground()).getColor(); 
Các vấn đề liên quan