最近最一個feature,要把drawable中的圖片資源儲存到資料庫,開始想儲存drawable ID,可id是會變化的;後來想儲存drawable的名稱,這樣不好取得resource。最後決定使用 image level來做,很簡單,只要存一組int 數就可以了,在顯示圖片的時候只要編寫一個xml檔案,再配合資料庫查出得image level就可以顯示出相應的圖片。資料庫就不說了,把image level 的XML檔案貼出來:
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:maxLevel="0" android:drawable="@drawable/mobile_popcaticon_generic" />
<item android:maxLevel="1" android:drawable="@drawable/mobile_popcaticon_bars_clubs" />
<item android:maxLevel="2" android:drawable="@drawable/mobile_popcaticon_restaurants" />
<item android:maxLevel="3" android:drawable="@drawable/mobile_popcaticon_gas_stations" />
<item android:maxLevel="4" android:drawable="@drawable/mobile_popcaticon_fast_food" />
<item android:maxLevel="5" android:drawable="@drawable/mobile_popcaticon_coffee" />
<item android:maxLevel="6" android:drawable="@drawable/mobile_popcaticon_shopping" />
<item android:maxLevel="7" android:drawable="@drawable/mobile_popcaticon_entertainment" />
<item android:maxLevel="8" android:drawable="@drawable/mobile_popcaticon_home_imp" />
<item android:maxLevel="9" android:drawable="@drawable/mobile_popcaticon_taxi" />
<item android:maxLevel="10" android:drawable="@drawable/mobile_popcaticon_pizza" />
<item android:maxLevel="11" android:drawable="@drawable/mobile_popcaticon_auto_parts" />
<item android:maxLevel="12" android:drawable="@drawable/mobile_popcaticon_groceries" />
<item android:maxLevel="13" android:drawable="@drawable/mobile_popcaticon_hospitals" />
<item android:maxLevel="14" android:drawable="@drawable/mobile_popcaticon_italian_food" />
<item android:maxLevel="15" android:drawable="@drawable/mobile_popcaticon_chinese_food" />
<item android:maxLevel="16" android:drawable="@drawable/mobile_popcaticon_pharmacies" />
<item android:maxLevel="17" android:drawable="@drawable/mobile_popcaticon_flowers" />
<item android:maxLevel="18" android:drawable="@drawable/mobile_popcaticon_pet_supplies" />
<item android:maxLevel="19" android:drawable="@drawable/mobile_popcaticon_deli" />
<item android:maxLevel="20" android:drawable="@drawable/mobile_popcaticon_hotels" />
<item android:maxLevel="21" android:drawable="@drawable/mobile_popcaticon_electronics" />
<item android:maxLevel="22" android:drawable="@drawable/mobile_popcaticon_breakfast" />
<item android:maxLevel="23" android:drawable="@drawable/mobile_popcaticon_auto_repair" />
<item android:maxLevel="24" android:drawable="@drawable/mobile_popcaticon_mexican_food" />
<item android:maxLevel="25" android:drawable="@drawable/mobile_popcaticon_delivery" />
<item android:maxLevel="26" android:drawable="@drawable/mobile_popcaticon_banks" />
<item android:maxLevel="27" android:drawable="@drawable/mobile_popcaticon_office_supplies" />
<item android:maxLevel="28" android:drawable="@drawable/mobile_popcaticon_towing" />
<item android:maxLevel="29" android:drawable="@drawable/mobile_popcaticon_beauty" />
</level-list>
2還要button的click, focus, press等顯示不同狀態也可以用XML完成:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_window_focused="false"
android:drawable="@drawable/mobile_btn_off" />
<item android:state_pressed="true"
android:drawable="@drawable/mobile_btn" />
<item android:state_focused="true"
android:drawable="@drawable/mobile_btn" />
<item
android:drawable="@drawable/mobile_btn_off" />
</selector>