0

Je souhaite créer un sélecteur dans Android Studio. J'ai créé un fichier nommé "button_hover.xml" dans le dossier res.layout.Création d'un sélecteur


button_hover.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_height="wrap_content" android:layout_width="wrap_content"> 
    <item android:state_focused="true" android:drawable="@drawable/bg1.png"/> 
    <item android:state_pressed="true" android:drawable="@drawable/bg2.png" /> 
    <item android:drawable="@drawable/bg3.png" /> 
</selector> 

bg1.png, bg2.png, bg3.png est existait dans mon dossier drawable, je peux utiliser Ctrl + Espace pour les appeler, mais quand Je cours l'application, la console montre l'erreur:

Error:(5, 58) No resource found that matches the given name (at 'drawable' with value '@drawable/bg1.png'). 
Error:(6, 58) No resource found that matches the given name (at 'drawable' with value '@drawable/bg2.png'). 
Error:(7, 29) No resource found that matches the given name (at 'drawable' with value '@drawable/bg3.png'). 

Comment puis-je résoudre ce problème?

+2

utilisation '@ drawable/bg1' à la place, perdre la' .png' – eriuzo

+1

supprimer l'extension .png d'où l'drawable utilisé –

Répondre

0

Pouvez-vous s'il vous plaît vérifier qu'ils sont créés sous res/drawable, il semble qu'ils sont sous de l'un de ces dossiers:

/drawable-ldpi For low density screens. 
/drawable-mdpi For medium density screens. 
/drawable-hdpi For high resolution screens. 
/drawable-xhdpi For extra high resolution screens. 

Si vous utilisez Android puis sélectionnez Projet d'un peu haut de la structure et vous voir votre draw-able sont sous quel dossier, ils devraient être sous draw-able si vous n'utilisez pas toutes les tailles.

0

sélecteur XML

<?xml version="1.0" encoding="utf-8"?> 

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" 
     android:drawable="@drawable/select_true" /> 

    <item android:state_focused="true" 
     android:drawable="@drawable/select_false" /> 

    <item android:drawable="@drawable/select_false" /> 
</selector> 

select_true XML

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" > 

<solid 
    android:color="#44ffffff"></solid> 

<stroke android:color="#ffffff" 
    android:width="1.2dp"/> 

</shape> 

XML select_false

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle" > 

<solid 
    android:color="#00ffffff"></solid> 

<stroke android:color="#ffffff" 
    android:width="1.3dp"/> 

</shape>