圖片設定level-list,根據不同狀態顯示不同圖片

青樓艾小生發表於2018-01-31

前言:工作需求,wifi圖示根據不同的強度,顯示不同的狀態.於是想到用圖層的方法

bg.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/icon_stop_n"></item>
    <item android:maxLevel="1" android:drawable="@drawable/icon_pause"></item>

</level-list>

note:android:maxLevel 必須從0遞增,順序錯誤後只會顯示第一張圖片

佈局檔案

 <Button
                android:id="@+id/bt_pause_bt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="115px"
                android:background="@drawable/bg"
                android:tag="play" />

程式碼中使用

LevelListDrawable pauseDrawable = (LevelListDrawable) yourwiget
                .getBackground();
pauseDrawable.setLevel(1);//根據業務需要,對應圖片等級


相關文章