Android从XML中的级别列表中选择一个drawable

时间:2013-02-25 08:14:40

标签: android xml android-drawable xml-drawable

如何为android:drawable属性选择 in XML ,其中一个是在level-list XML中定义的drawable? 我在level-list XML本身中定义了drawable,以避免定义许多小的XML文件。

<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:maxLevel="0">
        <shape 
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <gradient 
                android:startColor="@color/empty" 
                android:endColor="@android:color/white"
                android:angle="90" />

        </shape>
    </item>

    <item android:maxLevel="1">
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle" >
            <gradient 
                android:startColor="@color/low"
                android:endColor="@android:color/white"
                android:angle="90" />

        </shape>
    </item>

    <item android:maxLevel="2">
        <shape 
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <gradient 
                android:startColor="@color/middle"
                android:endColor="@android:color/white"
                android:angle="90" />

        </shape>
    </item>

    <item android:maxLevel="3">
        <shape 
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <gradient 
                android:startColor="@color/high"
                android:endColor="@android:color/white"
                android:angle="90" />

        </shape>
    </item>

    <item android:maxLevel="4">
        <shape 
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <gradient 
                android:startColor="@color/full"
                android:endColor="@android:color/white"
                android:angle="90" />

        </shape>
    </item>

</level-list>

1 个答案:

答案 0 :(得分:2)

这是不可能的。我只是在考虑相同的想法,期待像ImageView的属性。类似于Android:level

但不,setLevel方法在XML属性中没有等价物。您(和我)必须以编程方式调用setLevel