2013-02-25 8 views
2

Как выбрать в XML, для свойства android:drawable, одного из чертежей, определенных в XML-уровне на уровне?
Я определяю чертежи в самом XML-уровне, чтобы избежать определения множества небольших XML-файлов.Android Выбор выделенного из уровня списка в 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> 

ответ

2

Невозможно. Я просто искал ту же идею, ожидая чего-то вроде атрибута для ImageView. Что-то вроде Android:level.

Но нет, метод setLevel не имеет эквивалента в атрибутах XML. Вы (и я) должны программно вызвать setLevel.

0

В API 24 (и выше) эта функция теперь поддерживается; android:level, найденный в элементе <scale>, который определяет начальный уровень вытягивания в диапазоне от 0 до 10000 и может использоваться в <layer-list> как таковой;

<?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

программно