2011-05-07 29 views
2

在Android应用程序中水平和垂直重复线性布局的背景非常简单。您只需要使用tileMode(请参见How to make android app's background image repeat如何在Android应用程序中添加垂直平铺(仅)背景

但是,xml文件中的tileMode不允许我仅在垂直轴上重复背景图像。有一个tileModeY,但你不能从xml访问它。

我试图修改该属性以“重复”。

我已经与线性布局的main.xml中的文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/mainMenuLayout" 
       android:orientation="vertical" 
       android:gravity="center_horizontal" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 

我在绘制/ welcomebackground.png一个形象,也为绘制/ welcomebackgroundpattern.xml像一个XML文件:

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

在我的活动我已经试过这样:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Resources res = getResources(); 
    BitmapDrawable background = (BitmapDrawable) res.getDrawable(R.drawable.welcomebackground); 
    background.setTileModeY(Shader.TileMode.REPEAT); 
    (new View(this)).setBackgroundResource(R.drawable.welcomebackground); 
    } 

但我的背景仍然是全黑的。当然,我可以从xml中添加它,但它在x和y轴都重复。

所以我的问题很简单:我如何在我的活动中垂直重复“welcomebackground”图像?

+0

可能重复http://stackoverflow.com/questions/3657793/android-bitmap-tile-on-x-only ) – fredley 2011-05-16 17:54:22

回答