如何向Android应用添加垂直平铺(仅)背景

时间:2011-05-07 15:13:28

标签: android eclipse layout background

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

然而,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">

我在drawable / welcomebackground.png中有一个图像,还有一个用于drawable / 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”图像?

1 个答案:

答案 0 :(得分:1)

我找到了解决方案:Android Bitmap Tile on X Only