问题
I'm trying to get my tabicon to change when a tab is pressed (i.e. when it changes color when you press the tab, but haven't released yet). I've created a selector as follows:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false"
android:state_selected="false"
android:state_pressed="false"
android:drawable="@drawable/ic_tab_icon1" />
<item android:state_focused="false"
android:state_selected="true"
android:state_pressed="false"
android:drawable="@drawable/ic_tab_icon2" />
<!-- Focused states -->
<item android:state_focused="true"
android:state_selected="false"
android:state_pressed="false"
android:drawable="@drawable/ic_tab_icon3" />
<item android:state_focused="true"
android:state_selected="true"
android:state_pressed="false"
android:drawable="@drawable/ic_tab_icon4" />
<!-- Pressed -->
<item android:state_pressed="true"
android:drawable="@drawable/ic_tab_icon5" />
</selector>
However, for some reason only the first two states are reached (only icon1 and icon 2 are used). Can anyone tell me what the correct state is for a "pressed but not selected" tab?
*Edited to clarify new situation
回答1:
Here is tab_indicator.xml
from Android 2.2:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_focus" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_focus" />
<!-- Pressed -->
<item android:state_pressed="true" android:drawable="@drawable/tab_press" />
</selector>
You do not have anything where android:state_pressed="true"
. Try changing your selector to look a bit more like what Android uses.
回答2:
this is the default tab_indicator.xml
for android. got from this gitkernal. you have to maintain all these states.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/ic_tab_north_normal" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/ic_tab_north_selected" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/ic_tab_north_normal" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/ic_tab_north_selected" />
<!-- Pressed -->
<item android:state_pressed="true" android:drawable="@drawable/ic_tab_north_normal" />
</selector>
来源:https://stackoverflow.com/questions/3725692/whats-the-state-of-a-pressed-tab-in-android