2012-06-26 73 views
7

我有一張圖像需要水平平鋪,我需要通過編程完成。我嘗試了兩種方法,但都不起作用。Android以編程方式平鋪圖像作爲背景

button_inner_shadow是圖像 navigation_background是假設平鋪上述圖像

1的XML:平鋪圖像直接

BitmapDrawable navigationBackground = new BitmapDrawable(BitmapFactory.decodeResource(
       getResources(), R.drawable.button_inner_shadow)); 
navigationBackground.setTileModeX(Shader.TileMode.REPEAT); 
navigationTextViews[id].setBackgroundDrawable(navigationBackground); 

2:使用XML來平鋪圖像

navigationBackground = new BitmapDrawable(BitmapFactory.decodeResource(
       getResources(), R.drawable.navigation_background)); 
navigationTextViews[id].setBackgroundDrawable(navigationBackground); 

navigation_background

<?xml version="1.0" encoding="utf-8"?> 
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" 
    android:dither="true" 
    android:src="@drawable/button_inner_shadow" 
    android:tileMode="repeat" /> 

我在做什麼錯?

我也使用setBackgroundResource設置改變程序的另一部分的背景顏色,並認爲這是一個問題。我添加了navigationTextViews[id].setBackgroundResource(0);,它應該刪除背景資源,並且不適用於我使用的上述解決方案。

編輯:navigationTextViews []是TextViews

數組
+0

你得到了什麼錯誤,發佈logcat? –

+0

我沒有收到錯誤。 – heero

回答

3

那麼,出於某種原因,第一個選項有不同的圖像工作,所以我猜測的問題是,我的第一個圖像(這是一個梯度一些透明度)太透明瞭。

相關問題