2011-11-11 6 views
33

में एक बटन का पृष्ठभूमि रंग प्राप्त करें मुझे एक बटन का पृष्ठभूमि रंग कैसे मिलता है। गतिविधि कक्षा में अब पृष्ठभूमि = XXXXX मैं कैसे यह मान यह है कि प्राप्त कर सकते हैं: एक्सएमएल में मैं पृष्ठभूमि रंग का उपयोग कर ---- एंड्रॉयड सेट?एंड्रॉइड

उत्तर

73

दुर्भाग्य से मुझे नहीं पता कि वास्तविक रंग कैसे प्राप्त किया जाए।

यह एक Drawable

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

के रूप में इस प्राप्त करने के लिए आप जानते हैं कि यह एक रंग

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

है तो आप की कोशिश कर सकते हैं आसान है और अगर आप एंड्रॉयड पर हैं 3.0+ आप बाहर निकल सकते हैं रंग की संसाधन आईडी।

int colorId = buttonColor.getColor(); 

और इसकी तुलना अपने निर्दिष्ट रंगों से करें, यानी।

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

क्या आप वाकई getColor (हैं) आईडी हो जाता है? मुझे लगता है कि यह रंग का वास्तविक int मान प्राप्त करता है (यानी 0xAARRGGBB)। मैंने इसे "# 00000001" के साथ परीक्षण किया और यह वापस लौटा 1. – brianestey

+1

मेरे परीक्षण में मैंने 'बटन.सेट बैकग्राउंड (R.color.green)' किया और फिर प्रतिक्रिया की जांच की और यह निश्चित रूप से वास्तविक रंग आईडी नहीं था। मैं एक उचित रंग पूर्णांक पसंद करूंगा ताकि मैं 'Color.red (int)', 'Color.green (int) ',' Color.blue (int) 'कर सकता हूं। लेकिन एंड्रॉइड 3.2 पर मेरे परीक्षण में, यह मामला नहीं था।मुझे लगता है कि यह असंगत है, संदर्भ के आधार पर एक रंग int, या resid लौट रहा है। –

+1

यह केवल एपीआई 11+ –

2

पृष्ठभूमि Drawable पाने के लिए आपको

public Drawable getBackground(); 

का उपयोग के रूप में आधार View कक्षा में परिभाषित किया।

भूल Button एक पृष्ठभूमि है कि एक छवि, एक रंग, एक ढाल है हो सकता है मत करो। आप एंड्रॉयड का उपयोग करते हैं: पृष्ठभूमि = "# FFFFFF", पृष्ठभूमि के वर्ग होगा

android.graphics.drawable.ColorDrawable

वहां से आप बस फोन कर सकते हैं

public int getColor() 
14

तुम भी

android:tag="#ff0000" 

की तरह टैग के रूप में रंग मान सेट की तरह कुछ और यह एक्सेस कोड

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

काम नहीं करता है यह स्वीकार्य उत्तर होना चाहिए – IHeartAndroid

5

से simpliest रास्ता पाने के लिए कोशिश कर सकते हैं मेरे लिए रंग है:

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

परीक्षण किया गया और लॉलीपॉप 5.1.1

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