2

J'ai un View que j'ai créé par programmation, et je veux avoir un effet d'entraînement lorsque je le sélectionne. J'ai été en mesure d'obtenir ce travail en utilisant ?attr/selectableItemBackground. Cependant, je veux également définir la couleur de fond du View quand je le sélectionne. J'ai essayé setBackgroundResource(selectableAttr) puis setBackgroundColor(colorSelectBackground), mais la couleur semble écraser la ressource, donc je n'ai que l'un ou l'autre. Voici mon code:Ajouter? Attr/selectableItemBackground pour afficher et définir la couleur d'arrière-plan

int[] attrs = new int[]{R.attr.selectableItemBackground}; 
TypedArray typedArray = context.obtainStyledAttributes(attrs); 
int backRes = typedArray.getResourceId(0, 0); 

public void select() { 
    view.setSelected(true); 
    view.setBackgroundResource(backRes); 
    view.setBackground(colorSelectBackground); 
} 

public void deselect() { 
    view.setSelected(false); 
    view.setBackground(colorSelectBackground); 
} 

Quelqu'un sait comment je peux utiliser les deux ?attr/selectableItemBackground et également définir une couleur d'arrière-plan? Merci!

EDIT: Pour clarifier, la vue en question n'est pas un bouton, c'est RelativeLayout.

MISE À JOUR: Je n'ai jamais vraiment trouvé une bonne solution pour cela. Le plus proche que je suis utilisais View.setForeground() à un Drawable du TypedArray, à savoir

view.setForeground(typedArray.getDrawable(0)); 

Le principal inconvénient est qu'il est disponible uniquement sur API 23+. Faites-moi savoir si vous trouvez une meilleure solution.

+0

Voir http://stackoverflow.com/questions/26686250/material-effect-on-button-with-background-color –

+0

Merci @ ChantellOsejo, mais je n'ai pas eu de chance avec cette réponse, et je n'utilise pas un 'Button' de toute façon. – weirdo16

Répondre

1

Je vous recommande de créer un View personnalisé, où vous pouvez obtenir le pressedColor, defaultColor et disabledColor du xml.

Le code suivant travaillerait pour un matériau bouton style:

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 
{ 
    ColorStateList colorStates = new ColorStateList(
      new int[][]{ 
        new int[]{android.R.attr.state_pressed}, 
        new int[]{} 
      }, 
      new int[]{ 
        pressedColor, 
        defaultColor}); 

    view.setBackgroundDrawable(isEnabled ? new RippleDrawable(colorStates, getBackground(), getBackground()) 
      : new ColorDrawable(disabledColor); 
} 
else 
{ 
    StateListDrawable backgroundDrawable = new StateListDrawable(); 
    backgroundDrawable.addState(new int[]{android.R.attr.state_pressed}, new ColorDrawable(isEnabled ? 
      pressedColor : disbledColor)); 
    backgroundDrawable.addState(StateSet.WILD_CARD, new ColorDrawable(isEnabled ? defaultColor : 
      disabledColor)); 
    view.setBackgroundDrawable(backgroundDrawable); 
} 
+0

Merci Ognian, à la fois 'RippleDrawable' et' StateListDrawable' montrent l'effet d'entraînement, mais la couleur d'arrière-plan ne change pas. – weirdo16