xml background don't change when i use tablet

我怕爱的太早我们不能终老 提交于 2019-12-30 08:23: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?


回答1:


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.




回答2:


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

  • layout
  • layout-800x480
  • layout-land
  • etc. refer here http://developer.android.com/guide/practices/screens_support.html

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.




回答3:


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:


I used :

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

it's working with very fine



来源:https://stackoverflow.com/questions/14644517/xml-background-dont-change-when-i-use-tablet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!