在 Android 中使用 XML 中的 ImageView 平铺图像
我正在尝试将背景上的图像平铺,直到背景填满。
我当前的代码是:
<?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文件。
I am trying to get an image on the background to tile until the background is full.
My current code is:
<?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>
However that just makes the image covering (not tiled) bottom to top, but NOT left to right. What should I be doing?
Edit: Attempted the XML version:
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:tileMode="repeat"
android:src="@drawable/cartoonclouds" />
The main.xml could find the cartoonclouds in the SAME folder as that XML file, but could not find that XML file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,将图像放入 res/drawable/cloud.png 等文件中。这使得您的应用程序可以将其作为 @drawable/cloud 进行访问。但它还没有平铺。
接下来,您应该使用 android:tileMode="repeat" 定义位图资源(1)。例如,您可以在 mytileablebitmap.xml 上定义它:
这是引用您的原始平铺图像(可绘制/云)并制作一个知道如何平铺自身以填充可用空间的可绘制对象。
然后,当您想使用平铺背景时,请使用“@drawable/mytileablebitmap”。
(1): http://developer.android.com/guide/topics/resources/drawable-资源.html
First, place your image in a file like res/drawable/cloud.png. This makes it accessible to your app as @drawable/cloud. But it doesn't tile (yet).
Next, you should define a bitmap resource(1) with android:tileMode="repeat". For example, you can define it on mytileablebitmap.xml:
This is referencing your original tile image (drawable/cloud) and making a Drawable that knows how to tile itself to fill the available space.
Then, when you want to use the tiled background, use "@drawable/mytileablebitmap".
(1): http://developer.android.com/guide/topics/resources/drawable-resource.html