Android制作由多个位图组成的drawable XML

9

我在我的应用中有很多LinearLayout都使用了这个背景:

android:background="@drawable/metal_plate"

drawable\metal_plate.xml:

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

我希望在金属板的四个角上放置四个螺丝的位图。
由于我在几个地方都会使用这个金属板,因此我更喜欢将其定义为单个可绘制对象,而不是每次都需要使用 RelativeLayout 放置四个螺丝。

是否有可能在 drawable 文件夹中定义一个 XML 来组合平铺的金属板和四个螺丝?

2个回答

27

很遗憾,我现在无法真正测试这个,但我相信你可以使用LayerListDrawable来实现这个效果:

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <bitmap
            android:src="@drawable/diamond_metal"
            android:tileMode="repeat" />
    </item>

    <item android:right="10dp" android:bottom="10dp">
        <bitmap
            android:src="@drawable/screw"
            android:gravity="bottom|right" />
    </item>

    <item android:left="10dp" android:bottom="10dp">
        <bitmap
            android:src="@drawable/screw"
            android:gravity="bottom|left" />
    </item>

    <item android:top="10dp" android:left="10dp">
        <bitmap
            android:src="@drawable/screw"
            android:gravity="top|left" />
    </item>

    <item android:top="10dp" android:right="10dp">
        <bitmap
            android:src="@drawable/screw"
            android:gravity="top|right" />
    </item>
</layer-list>

将10dp的值替换成你需要用于螺钉的任何插入值。


1

这可以很容易地使用NinePatch完成。您可以创建一个NinePatch可绘制对象,然后将其设置为任何要具有背景的布局的背景。这只需要创建背景的正方形版本,然后建议使用Draw 9-Patch工具将其转换为Android可用的.9.png文件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接