我已经为我的android按钮创建了我的9补丁图像,并寻找了关于如何在应用程序中使用它们的教程(我使用的是android Studio btw),但我还找不到任何关于如何做到这一点的合理解释。我找到了一个我认为是直接的,但没有成功。因此,有人能把我链接到,或者简单地解释如何使用9个补丁文件(正常、聚焦、按下、禁用)吗?
编辑:要明确。正如我所说,我已经创建了我的4个图像(用于按钮的4种不同状态)。我不知道该怎么做才能将应用程序中的这些图像用作按钮。我应该采取哪些步骤来使用这些图像?
ACTIVITY.XML
<button
android:style="@drawable/button_default"/>
STYLES.XML
<style name="button_default">
<item name="android:background">@drawable/buttons</item>
</style>
BUTTONS.XML
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/default_disabled"
android:state_enabled="false"/>
<item
android:drawable="@drawable/default_pressed"
android:state_pressed="true"
android:state_enabled="true"/>
<item android:drawable="@drawable/default_focused"
android:state_focused="true"
android:state_enabled="true"/>
<item android:drawable="@drawable/default_normal"
android:state_enabled="true"
android:state_pressed="false"
android:state_focused="false"/>
</selector>
我的drawables是"drawablehdpi"。命名为"default_disabled.9.png"、"default_focused.9.png"、"default_normal.9.png"、"default_pressed.9.png"。
我哪里错了?
这需要三个步骤。下面是我的一个应用程序中的一个真实示例。
res/drawable/button_red.xml
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="true"
android:state_pressed="false"
android:drawable="@drawable/button_red_normal" />
<item
android:state_enabled="true"
android:state_pressed="true"
android:drawable="@drawable/button_red_pressed" />
<item
android:state_enabled="false"
android:drawable="@drawable/button_disabled" />
</selector>
每个可绘制文件(例如,@drawable/button_red_normal
)都是一个9补丁,每个分辨率(例如res/drawable-hdpi/button_red_normal.9.png
)具有不同的版本
在res/values/styles.xml
:中
<style name="button_common">
<item name="android:textColor">#ffffff</item>
<item name="android:layout_margin">3dp</item>
<item name="android:textSize">20sp</item>
<item name="android:textStyle">bold</item>
</style>
<style name="button_red" parent="@style/button_common">
<item name="android:background">@drawable/button_red</item>
</style>
我把它分为父子风格,因为我在应用程序中有不止一种颜色的按钮。他们的不同之处仅在于背景的可绘制性。
最后,将其添加到按钮的XML中:
style="@style/button_red"
九路径图像用于修复应用程序中的图像部分。简单地说,划痕问题就过去了。
http://romannurik.github.io/AndroidAssetStudio/nine-patches.html
使用此链接生成九个补丁图像。在矩形区域所在的地方,该部分是固定的。剩余区域正在刮伤。
当你下载图像时,你会得到不同的文件夹图像。请将图像放在android res.(如hdpi、xhdpi、mdpi)的同一文件夹中并使用。
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/thumb8" />