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

时间:2013-03-07 18:40:52

标签: android xml drawable

我的应用程序中有几个LinearLayout的背景知识:

android:background="@drawable/metal_plate"

抽拉\ 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>    

我想在金属板的4个角上放置4位螺丝 我在几个地方使用金属板,所以我更喜欢将它定义为一个可绘制的,而不是将{4}螺钉放在每个ocasion上RelativeLayout

是否可以在drawable文件夹中定义XML以组合平铺金属板和4个螺丝?

2 个答案:

答案 0 :(得分:26)

不幸的是我现在无法真正测试这个,但我相信你可以用这样的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 :(得分:1)

使用NinePatch可能很容易做到这一点。您可以创建一个NinePatch drawable,然后将其设置为您想要具有背景的任何布局的背景。这只需要您创建背景的方形版本,然后我建议使用Draw 9-Patch工具将其设置为.9.png以供Android使用。