2011-01-19 33 views
2

我有一個基本上包含TextView的客戶首選項窗口小部件(xml),並且在右側對齊的ImageButton旁邊。如何在Android中引用系統選擇按鈕圖像以進行自定義首選項

我想要做的是將我的圖像按鈕的圖像設置爲首選項系統默認使用的任何圖像。例如,請參閱附加的圖像,其中突出顯示了我想要使用的圖像類型。

我試過在網上查找並將ic_btn_round圖像保存到我的繪圖中並使用它,但它看起來不正確。

任何意見,歡迎,謝謝

alt text

回答

3

這個圈子按鈕來自DialogPreference。如果我們看看這種偏好是如何實現的,我們會發現,它使用在此XML文件描述插件:

<!-- 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" /> 

所以你需要尋找到2個文件:可繪製/ btn_circle和繪製/ 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> 

這兩個XML文件中提及的所有可繪有實際* .png文件。

所以基本上你需要使用兩個圖像來達到預期的效果。但是所有這些資源都是Android的內部資源,您不能直接使用它們。最簡單的方法就是將它們複製到您的項目中(就像您對ic_btn_round所做的那樣)。

+0

偉大的答案謝謝,這將有用的。 – Dave 2011-01-19 18:10:36