xml background don't change when i use tablet

2019-02-16 20:18发布

I have an xml that declare the button background:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/selector_pressed" />
    <item android:state_focused="true" android:drawable="@color/selector_focused" />
    <item android:drawable="@android:color/transparent" />
</selector>

On my phone Samsung Nexus S (android 4.0.3) , this xml work perfectly, when i press the view the background color change. But I got a problem on tablets (Samsung Galaxy tab, Android 3.2), the button is clicked but i don't see any changes for the backgroung. The background color never change for tablets.

Any one have an idea why this happening only for tablets? and how can i fix this?

4条回答
SAY GOODBYE
2楼-- · 2019-02-16 20:37

I used :

android:hardwareAccelerated="false"  in AndroidManifest.xml <application> tag

it's working with very fine

查看更多
成全新的幸福
3楼-- · 2019-02-16 20:44

try getting rid of the

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

could just be the way that version of OS intercepts pressed and focused. just a guess tho, didn't really test it.

Try changing your .xml to:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>         
            <solid
                android:color="#11223344" />
        </shape>
    </item>
    <item>
        <shape>
           <solid
               android:color="#AABBCCDD" />
        </shape>
    </item>
</selector>

of coures you can set your color to anything you'd like. That is if you are using static colors. you could add gradients and rounded edges and a border to this too.

查看更多
萌系小妹纸
4楼-- · 2019-02-16 20:45

I would suggest you to adjust your layout on each layout folder

and call your xml on each layout. It could be that when it was clicked in tablet, it was not displayed properly, I once experienced that issue too because the dimension or the resolution did not match, so the color is not displaying properly as well.

查看更多
做自己的国王
5楼-- · 2019-02-16 20:47

I've had the same problem but I've managed to solve this: the actual problem lies in a defect in android 3.2 regarding hardware acceleration so if you put in your AndroidManifest that your application is hardwareAccelerated="true" your problem will be solved.

查看更多
登录 后发表回答