调用XML drawables的XML按钮

时间:2011-07-13 13:41:40

标签: android android-layout android-xml

我在drawable文件夹上有一系列图像,我想在语言文件夹(drawable-pt)中重复使用,所以我为语言文件夹中的每个图像创建了一个xml文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/habitat_over" />

然后我在xml文件夹中有一个表示按钮状态的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
            android:drawable="@drawable/habitat_over"/>
    <item android:drawable="@drawable/habitat" />
</selector>

在布局中我有这样的东西:

<ImageButton android:layout_weight="1" 
android:background="@null" 
android:layout_width="wrap_content" 
android:src="@xml/habitat_button" 
android:id="@+id/habitat" 
android:layout_height="wrap_content" 
android:scaleType="fitCenter">
</ImageButton>

它应该可以工作但是当我运行应用程序时会出现此错误:

Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class <unknown>

E/AndroidRuntime(  409):    at android.view.LayoutInflater.createView(LayoutInflater.java:518)

E/AndroidRuntime(  409):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)

E/AndroidRuntime(  409):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)

E/AndroidRuntime(  409):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)

E/AndroidRuntime(  409):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)

E/AndroidRuntime(  409):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)

E/AndroidRuntime(  409):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)

E/AndroidRuntime(  409):    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)

E/AndroidRuntime(  409):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)

E/AndroidRuntime(  409):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)

E/AndroidRuntime(  409):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)

E/AndroidRuntime(  409):    at android.app.Activity.setContentView(Activity.java:1657)

E/AndroidRuntime(  409):    at pnm.freiramadeira.ChapterSelection.onCreate(ChapterSelection.java:90)

E/AndroidRuntime(  409):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

E/AndroidRuntime(  409):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

E/AndroidRuntime(  409):    ... 11 more

E/AndroidRuntime(  409): Caused by: java.lang.reflect.InvocationTargetException

E/AndroidRuntime(  409):    at java.lang.reflect.Constructor.constructNative(Native Method)

E/AndroidRuntime(  409):    at java.lang.reflect.Constructor.newInstance(Constructor.java:415)

E/AndroidRuntime(  409):    at android.view.LayoutInflater.createView(LayoutInflater.java:505)

E/AndroidRuntime(  409):    ... 25 more

E/AndroidRuntime(  409): Caused by: android.content.res.Resources$NotFoundException: File res/xml/habitat_button.xml from drawable resource ID #0x7f050007

E/AndroidRuntime(  409):    at android.content.res.Resources.loadDrawable(Resources.java:1697)

E/AndroidRuntime(  409):    at android.content.res.TypedArray.getDrawable(TypedArray.java:601)

E/AndroidRuntime(  409):    at android.widget.ImageView.<init>(ImageView.java:118)

E/AndroidRuntime(  409):    at android.widget.ImageButton.<init>(ImageButton.java:85)

E/AndroidRuntime(  409):    at android.widget.ImageButton.<init>(ImageButton.java:81)

E/AndroidRuntime(  409):    ... 28 more

E/AndroidRuntime(  409): Caused by: android.content.res.Resources$NotFoundException: File res/drawable-pt/habitat_over.xml from drawable resource ID #0x7f02002f

E/AndroidRuntime(  409):    at android.content.res.Resources.loadDrawable(Resources.java:1697)

E/AndroidRuntime(  409):    at android.content.res.Resources.getDrawable(Resources.java:581)

E/AndroidRuntime(  409):    at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)

E/AndroidRuntime(  409):    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)

E/AndroidRuntime(  409):    at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)

E/AndroidRuntime(  409):    at android.content.res.Resources.loadDrawable(Resources.java:1694)

E/AndroidRuntime(  409):    ... 32 more

E/AndroidRuntime(  409): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #2: <bitmap> requires a valid src attribute

E/AndroidRuntime(  409):    at android.graphics.drawable.BitmapDrawable.inflate(BitmapDrawable.java:375)

E/AndroidRuntime(  409):    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)

E/AndroidRuntime(  409):    at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)

E/AndroidRuntime(  409):    at android.content.res.Resources.loadDrawable(Resources.java:1694)

E/AndroidRuntime(  409):    ... 37 more

有谁知道为什么会这样?

提前致谢!!

1 个答案:

答案 0 :(得分:3)

即使没有真正记录,android:src的{​​{1}}属性也只接受图像文件(不是用XML定义的drawable)。

不要在位图中使用<bitmap>并从布局中引用src="@drawable/habitat_over",而是移除<bitmap>并从布局添加直接引用到<bitmap>。< / p>