使用Nine-Patch图像作为Android按钮的背景

3
我已经创建了以下 Nine-Patch PNG 图像,但是当我将其作为按钮的背景添加时,我得到了以下结果:如您所见,图片没有填充整个按钮背景。这是 XML 布局文件:

enter image description here

enter image description here

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="@drawable/ninepatchimage"
        android:text="Click ME" />
</LinearLayout>

有人能告诉我为什么吗?
2个回答

6

从图片上看,似乎这张图片没有被解析为9patch。如果不把它保存为img.9.png,它将会作为一张普通的图片出现。

就像这样:

enter image description here


也许可以尝试去掉填充。 - FabianCook
所选的图像被保存为img.9.png,但当我将其添加到项目作为图标集图像时,我检查了图像的名称,它在Drawable文件夹中的名称是img.png!!!问题出在这里吗? - Mohamad Ghanem
1
是的,它需要像上面的图片一样。 - FabianCook
你可以直接将图片拖到文件夹中。 - FabianCook
好的,我会尝试 :) 但是这个图片应该拖到哪个Drawable文件夹里呢? - Mohamad Ghanem
只需可绘制的资源,它将适用于所有密度,除非您有特定于语言的资源。 - FabianCook

0

现在在AndroidStudio中,您可以从资源中获取任何图像,右键单击它并选择"创建9-Patch文件",然后使用内置的9-Patch编辑器进行配置,如此处所述。


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