Level List簡介
下面是官方Drawable下的介紹
A Drawable that manages a number of alternate Drawables, each assigned a maximum numerical value. Setting the level value of the drawable with setLevel() loads the drawable resource in the level list that has a android:maxLevel value greater than or equal to the value passed to the method.
主要用level來控制顯示的圖層,對應的圖層的maxlLevel比設置的level大或相等時就顯示。我這裏使用是發現默認是使用使用最後一張圖層。
LevelListDrawable
A resource that manages a number of alternate Drawables, each assigned a maximum numerical value. Setting the level value of the object with setLevel(int) will load the image with the next greater or equal value assigned to its max attribute. A good example use of a LevelListDrawable would be a battery level indicator icon, with different images to indicate the current battery level.
從上面可以形象知道level list用於像電池圖標那樣有幾種狀態的地方使用。使用的時候。上面是XML中level-list的的實現類。
示例
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/status_off"
android:maxLevel="0" />
<item
android:drawable="@drawable/status_on"
android:maxLevel="1" />
</level-list>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:maxLevel="0" android:drawable="@drawable/ic_wifi_signal_1" />
<item android:maxLevel="1" android:drawable="@drawable/ic_wifi_signal_2" />
<item android:maxLevel="2" android:drawable="@drawable/ic_wifi_signal_3" />
<item android:maxLevel="3" android:drawable="@drawable/ic_wifi_signal_4" />
</level-list>
程序使用可以通過setLevel()
或setImageLevel()
來控制。相對於一些熱賣類的文字背景限定於特點的幾個顏色時就可以使用level-list來完成,比單純在代碼裏替換background好是背景圖片在level-list中,後期好維護,方便管理。圖片圖片中的升級和公告背景顏色不同,對於升級和公告的textView背景使用level-list可以實現。
int original_level = tvTest.getBackground().getLevel();
tvTest.getBackground().setLevel(original_level == 0 ? 1 : 0);