Android: Not more than 1 nine patch image at a time?
I am having problems with nine patch images ( **.9.png ). I have a widget layout开发者_JS百科 and would like to use nine patch images for the widget's backgroud. Here is my background.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/appwidget_bg" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/appwidget_bg" />
<item android:state_focused="true" android:drawable="@drawable/appwidget_bg" />
<item android:state_focused="false" android:drawable="@drawable/appwidget_bg" />
</selector>
The drawbales name is "appwidget_bg.9.png". This stuff works fine. But I would like to use different images for the focused and pressed states.
So I make it look like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/appwidget_bg_pressed" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/appwidget_bg_pressed" />
<item android:state_focused="true" android:drawable="@drawable/appwidget_bg_pressed" />
<item android:state_focused="false" android:drawable="@drawable/appwidget_bg" />
</selector>
The problem: As soon as I add the additional drawable "appwidget_bg_pressed.9.png" I get the following error message:
.../res/drawable/widget_bg.xml:21: ERROR Error: No resource found that matches the given name (at 'drawable' with value '@drawable/appwidget_bg').
.../AndroidManifest.xml:6: ERROR Error: No resource found that matches the given name (at 'icon' with value '@drawable/icon').
It seems like I can only add one nine patch drawable to the drawables folder. Why is that?
Are you certain they've been imported into your drawables folder? That error is saying it's not found the files. You can certainly have multiple 9-patch images. Seems like it's found appwidget_bg_pressed.9.png, but isn't seeing just appwidget_bg.9.png or icon.png
This is an old question, but I was facing same problem and I figured it out and solved my problem.
The problem was with 9-patch drawables; they had patches on one side instead of two. because I didn't want my drawable to stretch vertically but it seems that I can't do it with one side patching.
I hope it'll help others.
精彩评论