Есть ли diff @null Vs # 00000000
Есть ли какая-либо разница между прозрачным (@000000) @null Vs
в моем макете я установил android:background"@color/transparent"
но его показ другого другого цвета фона, который я использовал.
когда я использовал null свой рабочий тон.
Я хочу установить @null через программную.
как это сделать?
Ответы
Ответ 1
@null
означает отсутствие фона вообще (View.getBackground() возвращает null).
#00000000
означает, что у вас есть ColorDrawable в качестве фона, который имеет полностью прозрачный цвет.
Я не смотрел на код, но я предполагаю, что среда проверяет, прозрачна ли ColorDrawable и не рисует ее в этом случае. В противном случае у вас будет часть накладных расходов, что сделает @null
более быстрый выбор. Оба должны выглядеть одинаково, поэтому не уверен, что это ваш основной isse.
Чтобы установить эквивалент @null
в коде, используйте View.setBackgroundDrawable(null)
.
Ответ 2
Да, есть.
-
@null
означает отсутствие фона.
-
#00000000
означает добавление прозрачного фона.
Если у вас нет фона, сделайте его @null
, он должен работать лучше. Чтобы использовать @null
из кода, который вы можете попробовать:
widget.setBackgroundDrawable(null);
Ответ 3
установите 0 в фоновом режиме.
view.setBackgroundColor(0);
Ответ 4
Я бы сказал, что в большинстве случаев предпочитают @null фон над @android: цвет/прозрачность.
В коде используйте setBackground (null), который вызывает устаревший метод setBackgroundDrawable();
Если вы посмотрите на View.setBackgroundDrawable(), вы заметите, что если вы передадите null в качестве фона, он установит флаги в 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 minimum size could have affected this
// View layout, so let requestLayout
requestLayout = true;
}
computeOpaqueFlags();
if (requestLayout) {
requestLayout();
}
mBackgroundSizeChanged = true;
invalidate(true);
}