문제

My Android app, which uses a TabActivity to display its contents, has 4 tabs which all use a state-list drawable for the icon. They all have the same structure, just with different images:

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/icon_options_dark" android:state_selected="true" android:state_focused="false" />
        <item android:drawable="@drawable/icon_options_white" />
</selector>`

However, when selecting tabs with the trackball on my HTC Desire (green highlight), the dark image is used instead of the light one. As soon as a tab is actually selected, it does become white. What am I doing wrong?

도움이 되었습니까?

해결책

You need to have different items for different states. Combining several states in one item (as you did) is definitely wrong. Your XML can look like this:

<xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/icon_options_dark"
        android:state_selected="true" />
  <item android:drawable="@drawable/icon_options_light"
        android:state_focused="true" />
  <item android:drawable="@drawable/icon_options_white" />
</selector>

When it comes to drawing your drawable its state is firstly checked. If it is selected the first item will be used and that's it - no focused check will be performed (Android finds only first - not "most relevant" - state). But if it is not selected, the check for the focused state will be performed.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top