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);
7747

被折叠的 条评论
为什么被折叠?



