【问题标题】:Android Selecting a drawable from level-list in XMLAndroid 从 XML 中的级别列表中选择可绘制对象
【发布时间】:2013-02-25 08:14:39
【问题描述】:

对于android:drawable 属性,如何在 XML 中 选择在级别列表 XML 中定义的可绘制对象之一?
我在级别列表 XML 本身中定义可绘制对象,以避免定义许多小的 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>

【问题讨论】:

    标签: android xml android-drawable xml-drawable


    【解决方案1】:

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

    但是没有,setLevel 方法在 XML 属性中没有等效项。你(和我)必须以编程方式调用setLevel

    【讨论】:

      【解决方案2】:

      现在在 API 24(及更高版本)中支持此功能; android:level&lt;scale&gt; 元素中找到,它指定了 0 到 10000 范围内的初始可绘制级别,因此可以在 &lt;layer-list&gt; 中使用;

      <?xml version="1.0" encoding="utf-8"?>
      <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
          <item android:id="@android:id/background"
                android:gravity="center_vertical|fill_horizontal">
              <shape android:shape="rectangle"
                  android:tint="?attr/colorControlNormal">
                  <size android:height="4dp" />
                  <solid android:color="@android:color/white_disabled_material" />
              </shape>
          </item>
          <item android:id="@android:id/secondaryProgress"
                android:gravity="center_vertical|fill_horizontal">
              <scale android:scaleWidth="100%"
                  android:level="2000">
                  <shape android:shape="rectangle"
                      android:tint="?attr/colorControlActivated" >
                      <size android:height="4dp" />
                      <solid android:color="@android:color/black" />
                  </shape>
              </scale>
          </item>
          <item android:id="@android:id/progress"
                android:gravity="center_vertical|fill_horizontal">
              <scale android:scaleWidth="100%"
                  android:level="1000">
                  <shape android:shape="rectangle"
                      android:tint="?attr/colorControlActivated" >
                      <size android:height="4dp" />
                      <solid android:color="@android:color/white_disabled_material" />
                  </shape>
              </scale>
          </item>
      </layer-list>
      

      要动态更改级别,然后以编程方式调用 setLevel

      【讨论】:

        猜你喜欢
        • 2016-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-17
        • 1970-01-01
        • 2010-11-19
        • 1970-01-01
        • 2011-06-02
        相关资源
        最近更新 更多