9

我试图让背景上的图像平铺,直到背景已满。

我目前的代码是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/cartoonclouds"
    android:contentDescription="@string/desc"
    android:tileMode="repeat" />
  <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="@string/hello" />
  </LinearLayout>
</RelativeLayout>

然而,这只会使图像覆盖(不平铺)从下到上,而不是从左到右。我应该做什么?

编辑:尝试了 XML 版本:

<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:tileMode="repeat"
    android:src="@drawable/cartoonclouds" />

main.xml 可以在与该 XML 文件相同的文件夹中找到 cartoonclouds,但找不到该 XML 文件。

4

1 回答 1

36

首先,将您的图像放在 res/drawable/cloud.png 之类的文件中。这使得您的应用可以通过@drawable/cloud 访问它。但它不平铺(还)。

接下来,您应该使用 android:tileMode="repeat" 定义位图资源 (1)。例如,您可以在 mytileablebitmap.xml 上定义它:

<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:tileMode="repeat" 
android:src="@drawable/cloud"/>

这是引用您的原始平铺图像(可绘制/云)并制作一个知道如何平铺自身以填充可用空间的可绘制对象。

然后,当您想使用平铺背景时,请使用“@drawable/mytileablebitmap”。

(1):http: //developer.android.com/guide/topics/resources/drawable-resource.html

于 2012-01-17T18:45:48.943 回答