开发者

How to reference the system select button image for custom preference in Android

I have a customer preference widget (xml) which basically contains a TextView and next to that a right aligned ImageButton.

What I am trying to do is set the image for my image button to whatever is used as default by the preferences system. For example see the attached image where I've hi开发者_高级运维ghlighted the type of image I want to use.

I've tried looking online and saving the ic_btn_round image into my drawables and using that, but it just doesn't look right.

Any advice welcome, thank you

How to reference the system select button image for custom preference in Android


This circle button comes from DialogPreference. If we look into how this preference is implemented, we'll find, that it uses widget described in this xml file:

<!-- Layout used by DialogPreference widgets. This is inflated inside 
    android.R.layout.preference. -->
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="4dip"
    android:layout_gravity="center_vertical"
    android:background="@drawable/btn_circle"
    android:src="@drawable/ic_btn_round_more" />

So you need to look into 2 more files: drawable/btn_circle and drawable/ic_btn_round_more.

btn_cicle.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false" android:state_enabled="true"
        android:drawable="@drawable/btn_circle_normal" />
    <item android:state_window_focused="false" android:state_enabled="false"
        android:drawable="@drawable/btn_circle_disable" />
    <item android:state_pressed="true" android:state_enabled="false"
        android:drawable="@drawable/btn_circle_disable" />
    <item android:state_pressed="true" 
        android:drawable="@drawable/btn_circle_pressed" />
    <item android:state_focused="true" android:state_enabled="true"
        android:drawable="@drawable/btn_circle_selected" />
    <item android:state_enabled="true"
        android:drawable="@drawable/btn_circle_normal" />
    <item android:state_focused="true"
        android:drawable="@drawable/btn_circle_disable_focused" />
    <item
        android:drawable="@drawable/btn_circle_disable" />
</selector>

ic_btn_round_more.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false"
        android:drawable="@drawable/ic_btn_round_more_disabled" />
    <item
         android:drawable="@drawable/ic_btn_round_more_normal" />
</selector>

All drawables referenced from these two xml files are actual *.png files.

So basically you need to use two images to achieve desired effect. But all these resources are internal to Android, and you can't use them directly. The easiest way is to just copy them to your project (as you did with ic_btn_round ).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜