मैं कहूंगा, ज्यादातर परिस्थितियों में @android पर @null पृष्ठभूमि पसंद करते हैं: रंग/पारदर्शी।
कोड में, सेटबैकग्राउंड (शून्य) का उपयोग करें जो एक बहिष्कृत विधि सेट बैकग्राउंड ड्रायबल();
यदि आप View.setBackgroundDrawable() को देखते हैं, तो आप देखेंगे कि यदि आप पृष्ठभूमि के रूप में शून्य पास करते हैं तो यह झंडे को SKIP_DRAW पर सेट करेगा और यही वह है। दूसरी तरफ, यदि कोई ड्रॉइंग ऑब्जेक्ट है, तो यह पृष्ठभूमि पैडिंग सेट अप करने के लिए अतिरिक्त प्रक्रिया के माध्यम से जाएगा।
यहाँ setBackgroundDrawable का कोड है (नोट: उपयोग setBackground setBackgroundDrawable के बजाय)
public void setBackgroundDrawable(Drawable background) {
computeOpaqueFlags();
if (background == mBackground) {
return;
}
boolean requestLayout = false;
mBackgroundResource = 0;
/*
* Regardless of whether we're setting a new background or not, we want
* to clear the previous drawable.
*/
if (mBackground != null) {
mBackground.setCallback(null);
unscheduleDrawable(mBackground);
}
if (background != null) {
Rect padding = sThreadLocal.get();
if (padding == null) {
padding = new Rect();
sThreadLocal.set(padding);
}
resetResolvedDrawables();
background.setLayoutDirection(getLayoutDirection());
if (background.getPadding(padding)) {
resetResolvedPadding();
switch (background.getLayoutDirection()) {
case LAYOUT_DIRECTION_RTL:
mUserPaddingLeftInitial = padding.right;
mUserPaddingRightInitial = padding.left;
internalSetPadding(padding.right, padding.top, padding.left, padding.bottom);
break;
case LAYOUT_DIRECTION_LTR:
default:
mUserPaddingLeftInitial = padding.left;
mUserPaddingRightInitial = padding.right;
internalSetPadding(padding.left, padding.top, padding.right, padding.bottom);
}
mLeftPaddingDefined = false;
mRightPaddingDefined = false;
}
// Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
// if it has a different minimum size, we should layout again
if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
mBackground.getMinimumWidth() != background.getMinimumWidth()) {
requestLayout = true;
}
background.setCallback(this);
if (background.isStateful()) {
background.setState(getDrawableState());
}
background.setVisible(getVisibility() == VISIBLE, false);
mBackground = background;
if ((mPrivateFlags & PFLAG_SKIP_DRAW) != 0) {
mPrivateFlags &= ~PFLAG_SKIP_DRAW;
mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
requestLayout = true;
}
} else {
/* Remove the background */
mBackground = null;
if ((mPrivateFlags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0) {
/*
* This view ONLY drew the background before and we're removing
* the background, so now it won't draw anything
* (hence we SKIP_DRAW)
*/
mPrivateFlags &= ~PFLAG_ONLY_DRAWS_BACKGROUND;
mPrivateFlags |= PFLAG_SKIP_DRAW;
}
/*
* When the background is set, we try to apply its padding to this
* View. When the background is removed, we don't touch this View's
* padding. This is noted in the Javadocs. Hence, we don't need to
* requestLayout(), the invalidate() below is sufficient.
*/
// The old background's minimum size could have affected this
// View's layout, so let's requestLayout
requestLayout = true;
}
computeOpaqueFlags();
if (requestLayout) {
requestLayout();
}
mBackgroundSizeChanged = true;
invalidate(true);
}
अपने color.xml में गलती हो या कोशिश कर सकते हैं '@Android: रंग/transparent'। – MKJParekh
@ सोनी "00000000" और एंड्रॉइड से अलग है: रंग। दोनों एक ही सही हैं। –
बस सेटबैकग्राउंड कॉलर() के साथ उलझन में सभी के लिए धन्यवाद। –