我试图让背景上的图像平铺,直到背景满为止。
我当前的代码是:
<?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 文件相同的文件夹中找到卡通云,但找不到该 XML 文件。
最佳答案
首先,将您的图像放在一个文件中,例如 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"/>
这是引用您的原始图 block 图像(可绘制/云)并制作一个知道如何平铺自身以填充可用空间的可绘制对象。
然后,当你想使用平铺背景时,使用“@drawable/mytileablebitmap”。
(1):http://developer.android.com/guide/topics/resources/drawable-resource.html
关于android - 使用 XML 中的 ImageView 在 android 中平铺图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8899222/