2011-08-31 93 views
14

這是我想做的事:我有這種規模的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

回答

3

這是無法用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); 

我使用此解決方案測試你的代碼,它工作得很好。

相關問題