Tous des réponses à propos de la mise en couleur transparente fonctionnera techniquement. Mais il y a deux problèmes avec ces approches:
- Vous finirez avec overdraw.
- Il y a une meilleure façon:
Si vous regardez comment fonctionne View.setBackgroundColor(int color)
vous verrez une solution assez facile:
/**
* Sets the background color for this view.
* @param color the color of the background
*/
@RemotableViewMethod
public void setBackgroundColor(@ColorInt int color) {
if (mBackground instanceof ColorDrawable) {
((ColorDrawable) mBackground.mutate()).setColor(color);
computeOpaqueFlags();
mBackgroundResource = 0;
} else {
setBackground(new ColorDrawable(color));
}
}
La couleur int
est simplement converti en un ColorDrawable
puis passé à setBackground(Drawable drawable)
. Donc, la solution pour supprimer la couleur d'arrière-plan est d'annuler l'arrière-plan avec:
myView.setBackground(null);
Vous pouvez le définir en transparent: View.setBackgroundColor (0x00000000); Ou android.R.color.transparent – Juhani
juste découvert que, une autre façon de le définir à transparent serait, View.setBackgroundColor (Color.TRANSPARENT); – sat
Si vous voulez juste utiliser l'arrière-plan du parent, vous pouvez supprimer l'arrière-plan de l'enfant entier avec: view.setBackgroundResource (0); –