8

在我的整个应用程序中,我有几个LinearLayouts 的背景:

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 个螺丝位图。
我在几个地方使用了金属板,所以我更喜欢将它定义为一个可绘制的,而不是必须在每个场合都用RelativeLayout.

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

4

2 回答 2

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 值。

于 2013-03-07T18:54:36.493 回答
1

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

于 2013-03-07T18:45:02.877 回答