开发者

Problem with selector for a ListView in Android

开发者 https://www.devze.com 2023-01-18 09:27 出处:网络
I have created a ListView and applied a selector to it as follows <selector xmlns:android=\"http://schemas.android.com/apk/res/android\">

I have created a ListView and applied a selector to it as follows

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
    android:drawable="@drawable/my_btn_pressed" />         
<item android:state_focused="true"
    android:drawable="@drawable/my_btn_focussed" /> 
<item android:drawable="@d开发者_Go百科rawable/my_btn_normal" />      
</selector>

When focussed or pressed, the background of the ListView item comes as specified in the selector. But the default background is never applied, can you tell me what is wrong?

By the way, this is the customised row xml I've used:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip"
    android:textColor="#FFFFFF"
    android:textStyle="bold"
    android:textSize="20sp" >
</TextView>

Thanks, Kiki


The solution was presented as an answer to this question.


Try using this code:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_pressed="true" android:state_focused="false" android:drawable="@drawable/my_btn_pressed" />         
   <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/my_btn_focussed" /> 
   <item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/my_btn_normal" />      
</selector>


In your Textview, you should try to change this...

android:background="@drawable/<b>my_btn_normal</b>"

to this...

android:background="@drawable/<b>your selector's name</b>"
0

精彩评论

暂无评论...
验证码 换一张
取 消