Android:是否可以缩放xml drawable?

问题描述:

这是我想做的事:我有这种规模的xml:Android:是否可以缩放xml drawable?

<?xml version="1.0" encoding="utf-8"?> 
<scale xmlns:android="http://schemas.android.com/apk/res/android" 
    android:drawable="@drawable/mydrawable" 
    android:scaleGravity="bottom" 
    android:scaleHeight="50%" /> 

而且我想这个可绘制XML使用它:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape xmlns:android="http://schemas.android.com/apk/res/android" 
      android:shape="rectangle"> 
      <gradient android:startColor="#FFFFFFFF" 
       android:endColor="#FFFF0000" 
       android:angle="90" /> 
     </shape> 
    </item> 
</layer-list> 

然后我设置比例XML作为背景对于LinearLayout,但我什么都看不到。我是否错过了一些东西,或者不可能这样做(使用XML)?

+1

顺便说一句顶层元素可以是'shape'而不需要一个图层列表来包围它。另外,哪些资源目录是这些文件? – Greyson

+0

对不起,我刚刚意识到我很晚才进入这个_extremely_。 – Greyson

+0

@michelemarcon:你最终得到了什么? – Behnam

这是无法用XML解析的ScaleDrawable对象的问题。

但它很容易,只要做到以下几点:

// Get the scale drawable from your resources 
ScaleDrawable scaleDraw = (ScaleDrawable)getResources().getDrawable(R.drawable.background_scale); 
// set the Level to 1 (or anything higher than 0) 
scaleDraw.setLevel(1); 

// Now assign the drawable where you need it 
layout.setBackgroundDrawable(scaleDraw); 

我使用此解决方案测试你的代码,它工作得很好。