Android:是否可以缩放xml可绘制文件?

Android:是否可以缩放xml可绘制文件?,android,xml,scale,shape,Android,Xml,Scale,Shape,下面是我要做的:我有一个缩放xml: <?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"?>
<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>


然后我将scalexml设置为线性布局的背景,但我什么也看不到。我是否遗漏了什么,或者(使用XML)不可能做到这一点?

这是ScaledRavable对象的问题,无法用XML解决

但这很简单,只需执行以下操作:

// 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);

我用这个解决方案测试了你的代码,效果很好。

顺便说一句,顶层元素可以是“shape”,而不需要层列表来包围它。还有,这些文件在哪一个资源目录中?对不起,我刚刚意识到我来得太晚了。@michelemarcon:你有什么结果吗?