【问题标题】:Android Making a drawable XML composed of several bitmapsAndroid 制作由多个位图组成的可绘制 XML
【发布时间】:2013-03-07 18:40:51
【问题描述】:

我的应用程序中的多个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 放置 4 个螺钉。

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

【问题讨论】:

    标签: android xml drawable


    【解决方案1】:

    很遗憾,我现在无法真正测试这个,但我相信你可以使用像这样的 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 值替换为螺钉所需的任何插图。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2013-06-06
        • 1970-01-01
        • 1970-01-01
        • 2012-08-21
        • 1970-01-01
        • 1970-01-01
        • 2014-07-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多