安卓:如何从图案中创建一个背景?[英] Android: how to create a background from pattern?

本文是小编为大家收集整理的关于安卓:如何从图案中创建一个背景?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个模式(.png image 4x4px),必须用它填充布局.

有人知道该怎么做吗?

如果我简单地选择可绘制的图像作为背景图像,则将其拉伸;相反,它需要沿x和y轴重复.

推荐答案

在这里是一个非常好的解释:

将您的" Back.png"图像放在"可绘制"文件夹上.然后创建一个可绘制的" backrepeat.xml",例如:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/back" 
    android:tileMode="repeat" /> 

在您的布局中,添加android:background="@drawable/backrepeat":

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/MainLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/backrepeat">

</LinearLayout>

与许多Android良好的做法/方便的技巧一样,它可以追溯到Romain Guy.

本文地址:https://www.itbaoku.cn/post/627748.html

问题描述

I have a pattern (.png image 4x4px) and have to fill the layout with it.

Does anyone know how to do this?

If I simply select the drawable as a background the image, it is stretched; instead it needs to be repeated along the x and y axis.

推荐答案

Here is a really nice explanation:

Put your "back.png" image on "drawable" folder. Then create a drawable "backrepeat.xml" like that:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/back" 
    android:tileMode="repeat" /> 

In your layout, add android:background="@drawable/backrepeat":

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/MainLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/backrepeat">

</LinearLayout>

As is the case with many Android good practices/handy tricks, it can be traced back to Romain Guy.