2012-08-13 96 views
2

在我的活动的布局有一个背景,这应该由Y.重复没有与重复的背景下,当它被重复X和Y没问题(刚加入android:tileMode="repeat")。此外,还有用这样更高版本的Android API 15没有问题:仅适用于Android TileMode X OR Y?

RelativeLayout layout = (RelativeLayout) findViewById(R.id.container_layout); 

BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(R.drawable.my_repeating_bg); 
bg.setTileModeX(TileMode.REPEAT);` 
layout.setBackground(bg); 

但这种方法setBackground(<BitmapDrawable>)是不是可以在Android API 我应该怎么做时,我想只有X或重复我的背景由Y在Android 2.3? 谢谢。 P.S.背景的宽度和高度都是MATCH_PARENT

回答

4

好了,我找到了答案。我只需要检查版本并使用适当的方法。

RelativeLayout layout = (RelativeLayout) fragmentView.findViewById(R.id.container); 
    BitmapDrawable bg = (BitmapDrawable)fragmentView.getResources().getDrawable(R.drawable.background); 
    bg.setTileModeY(TileMode.REPEAT); 
    if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) 
     layout.setBackgroundDrawable(bg); 
    else 
     layout.setBackground(bg); 
5

您可以在drawable文件夹中的XML文件(或文件夹绘制真的),这是在这种格式。

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

然后做:

layout.setBackgroundResource(R.drawable.name_of_xml_drawable); 

如果布局的宽度设置为wrap_content,只有Y将被平铺。如果布局的高度设置为wrap_content,则只有X会平铺。这两个设置为match_parentfill_parent意志瓦X和Y.

+0

如果我想拉伸X'es,所以我的背景将覆盖所有屏幕,我想通过Y重复这些拉伸行? – lomza 2012-08-13 13:46:50

+0

你不能用瓷砖和背景做到这一点。在这种情况下,将您的'BitmapDrawable'设置为整个View背景中的'ImageView'。你需要把它放在一个'RelativeLayout'或其他可以重叠的布局中。 – DeeV 2012-08-13 14:04:06