Bouton créer un fond transparent à l'aide du sélecteur
Ici nous allons. J'ai un bouton qui est un non-état pressé arrière-plan transparent. Dès que le bouton est pressé, l'arrière-plan n'est plus transparent (voir les photos). J'utilise patch neuf pour toutes mes images.
C'est mon sélecteur:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/starfighterstartbtndown" android:state_pressed="true"/>
<item android:drawable="@drawable/starfighterstartbtn"/>
</selector>
Et voici la mise en page:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/mainMenuImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/starfighter" >
</ImageView>
<RelativeLayout
android:id="@+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="@android:color/transparent"
android:clickable="true"
android:hapticFeedbackEnabled="true"
android:src="@drawable/startselector" >
</ImageButton>
<ImageButton
android:id="@+id/btnExit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:clickable="true"
android:src="@drawable/exitselector"
android:background="@android:color/transparent"
android:hapticFeedbackEnabled="true" >
</ImageButton>
</RelativeLayout>
Pas Cliqué:
Cliqué:
J'ai aussi essayé les méthodes suivantes avec pas de chance
button.getBackground().setAlpha(0);
button.setBackgroundColor(Color.TRANSPARENT);
Aussi essayé le suivant à l'intérieur de la onClickListener()
view.getBackground().setAlpha(0);
view.setBackgroundColor(Color.TRANSPARENT);
button.getBackground().setAlpha(0);
button.setBackgroundColor(Color.TRANSPARENT);
Toujours pas de chance. Bonne chance 🙂
- Est-il possible que la deuxième image fichier de ressources pour le bouton n'est pas transparent, tandis que le premier est?
- Eh bien, c'est embarrassant. Oui le bouton "down" .png n'a pas un arrière-plan transparent. Si seulement les programmeurs comme moi ne pourrait le penser....
Vous devez vous connecter pour publier un commentaire.
pour faire de votre bouton de l'arrière-plan transparent dans non pressé état , le sélecteur doit être comme ceci :
Et dans votre imageButton de la déclaration xml doit utiliser le sélecteur de fond et à ne pas faire quelque chose en tant que source de votre imageButton:
Vous pouvez utiliser de la Couleur.argb(). Il prend quatre paramètres int, chacun allant de 0 à 255.
Disons que vous voulez un bouton bleu avec 50% alpha:
Vous pouvez également essayer cette