2017-03-14 5 views
1

J'essaie d'avoir un rectangle ToggleButton qui contient une icône, une fois cliqué, l'arrière-plan devrait changer de couleur, mais l'icône devrait rester visible.ToggleButton rectangle contenant une icône

Jusqu'à présent, je suis en mesure d'avoir une couleur de changement de rectangle sur un clic comme tel:

<ToggleButton 
    android:background="@drawable/toggle_selector" 
    android:id="@+id/toggle" 
    android:checked="false" 
    android:textOn="" 
    android:textOff="" 
    android:layout_width="60dp" 
    android:layout_height="60dp" /> 

toggle_selector.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:drawable="@drawable/toggle_state_on" 
     android:state_checked="true" /> 
    <item 
     android:drawable="@drawable/toggle_state_off" 
     android:state_checked="false" /> 
</selector> 

toggle_state_on.xml:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape android:shape="rectangle"> 
      <!-- Draw a 5dp width border around shape --> 
      <stroke 
       android:color="#4c975d" 
       android:width="5dp" 
       /> 
     </shape> 
    </item> 
    <!-- Overlap the left, top and right border using background color --> 
    <item 
     android:bottom="5dp" 
     > 
     <shape android:shape="rectangle"> 
      <solid android:color="#6dd988"/> 
     </shape> 
    </item> 
</layer-list> 

toggle_state_off étant le même que sur mais avec des couleurs différentes.

Je n'arrive pas à trouver comment mettre une icône au centre du bouton que j'ai créé, est-ce que cela peut être réalisé? Et comment?

+0

s'il vous plaît ont oeil à ce sujet, je crois qu'il répond à votre question http://stackoverflow.com/questions/18598255/android- create-a-toggle-button-with-image-and-no-text – chirag90

Répondre

1

Vous devrez peut-être modifier le code ci-dessous si nécessaire, mais cela aura une icône au centre pour l'état allumé et éteint. Ajouter la toggle_state_off ligne et toggle_state_on android:drawable="@drawable/ic_attach_money_black_24dp"

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape android:shape="rectangle"> 
      <!-- Draw a 5dp width border around shape --> 
      <stroke 
       android:color="#2c125d" 
       android:width="5dp" 
       /> 
     </shape> 
    </item> 
    <!-- Overlap the left, top and right border using background color --> 
    <item 
     android:bottom="5dp" 
     android:drawable="@drawable/ic_attach_money_black_24dp"> 
     <shape android:shape="rectangle"> 
      <solid android:color="#2c125d"/> 
     </shape> 
    </item> 
</layer-list> 

marquer comme réponse si cela aide

+0

Cela ajoute l'icône, mais supprime la couleur d'arrière-plan et ne conserve que les bordures. Comment puis-je conserver la couleur d'arrière-plan? EDIT: En outre, cela me limite à une icône, je ne serai pas en mesure de réutiliser ces fichiers d'état à bascule pour différents boutons. – Christopher

+0

image qui est transparente? peut-être, c'est pourquoi j'ai dit peut-être besoin de modifier au besoin – chirag90

+0

L'image doit être transparente car elle vient de https://material.io/icons/ – Christopher