Android:如何从图案创建背景?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1700099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:29:19  来源:igfitidea点击:

Android: how to create a background from pattern?

androidbackgrounddesign-patterns

提问by Niko Gamulin

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

我有一个图案(.png 图像 4x4px)并且必须用它填充布局。

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.

如果我只是选择可绘制对象作为图像的背景,它会被拉伸;相反,它需要沿 x 和 y 轴重复。

回答by Dimitar Dimitrov

Hereis a really nice explanation:

是一个非常好的解释:

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

将“back.png”图像放在“drawable”文件夹中。然后像这样创建一个可绘制的“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" /> 

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

在您的布局中,添加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.

与许多 Android 良好实践/方便的技巧一样,它可以追溯到 Romain Guy。