繁体   English   中英

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

[英]Android Making a drawable XML composed of several bitmaps

我在整个应用程序中有几个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位螺丝。
我在几个地方使用金属板,所以我更喜欢将它定义为一个可绘制的,而不是将每个ocasion上的4个螺钉放在RelativeLayout

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

不幸的是我现在无法真正测试它,但我相信你可以用这样的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值。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM