Android:是否可以扩展xml drawable?

时间:2011-08-31 11:49:11

标签: 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"?>
<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>

然后我将scale xml设置为LinearLayout的背景,但我什么都没看到。我错过了什么或者不可能这样做(使用XML)?

1 个答案:

答案 0 :(得分:3)

这是ScaleDrawable对象的问题,无法用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);

我使用此解决方案测试了您的代码,它工作正常。