2012-06-26 72 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

那么,出于某种原因,第一个选项有不同的图像工作,所以我猜测的问题是,我的第一个图像(这是一个梯度一些透明度)太透明了。

相关问题