Andoid Layer-list Drawable

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可以实现。

notice

int original_level = tvTest.getBackground().getLevel();
tvTest.getBackground().setLevel(original_level == 0 ? 1 : 0);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值