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


67

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

有谁知道如何做到这一点?

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

Answers:


180

这是一个非常好的解释:

将您的“ 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" /> 

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


不错的答案!正是我需要的!
— 2014年

但对我来说,它仅适用于android studio的预览模式
— Jemo Mgebrishvili

如何将圆角和重复的背景添加到布局中?
— Wijay Sharma
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.