在 Android 中使用 XML 中的 ImageView 平铺图像

发布于 2024-12-27 15:26:03 字数 1131 浏览 3 评论 0原文

我正在尝试将背景上的图像平铺,直到背景填满。

我当前的代码是:

<?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

香草可樂 2025-01-03 15:26:03

首先,将图像放入 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-资源.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:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文