开发者

State maintainance for a button (Resource handling) in Android?

I have achieved that to maintain the state of a button using selector XML component. See the code snippet for example.

<?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"
        androi开发者_如何学运维d:state_pressed="false" android:drawable="@drawable/back_arrow_normal" />
    <item android:state_focused="false" android:state_selected="true"
        android:state_pressed="false" android:drawable="@drawable/back_arrow_hover" />
    <!--  Focused states 
  -->
    <item android:state_focused="true" android:state_selected="false"
        android:state_pressed="false" android:drawable="@drawable/back_arrow_hover" />
    <item android:state_focused="true" android:state_selected="true"
        android:state_pressed="false" android:drawable="@drawable/back_arrow_hover" />
    <!--  Pressed 
  -->
    <item android:state_pressed="true" android:drawable="@drawable/back_arrow_hover" />
</selector>

Using this idea, we should have a minimum of two images. If I have ten buttons in a project then I should have twenty images in the resource drawable. To make support multiple density/screen size to the application then I need 60 (20X3) images.

I have two kind of thoughts, but I don't know how to implement them. They are:

  1. we have to define a method instead of selector file.
  2. we should have a single image and we need to change alpha value like iPhone.

Is there any other great Ideas are most needed. Because this increases the size of the application.

Please express your thoughts and ideas.


I guess, your button's backgrounds are not too complex. If they are not, then you can use 9-patch pngs, so, the number of drawables would reduce to just one set. 9-patch pngs would resize and scale as per the need.

But, if the bckgrounds are complex, multiple resources for each resolution is required.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜