我正在尝试将图像添加到图像按钮。 我的button.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/button_pressed" />
<item
android:state_focused="true" android:drawable="@drawable/button_focused" />
<item
android:drawable="@drawable/button_normal" />
</selector>
我的main.xml看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageButton
android:src="@drawable/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
我的错误是这样的:
org.xmlpull.v1.XmlPullParserException: Binary XML file line #4: <item> tag requires a 'drawable' attribute or child tag defining a drawable
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:151)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:749)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:695)
at com.android.layoutlib.bridge.ResourceHelper.getDrawable(ResourceHelper.java:145)
at com.android.layoutlib.bridge.BridgeTypedArray.getDrawable(BridgeTypedArray.java:662)
at android.widget.ImageView.<init>(ImageView.java:113)
at android.widget.ImageButton.<init>(ImageButton.java:51)
at android.widget.ImageButton.<init>(ImageButton.java:47)
我知道错误集中在button.xml中的第一个“item”标记。我已经尝试在main.xml中将src更改为background,我尝试在button.xml中将drawable更改为background,但每次都会出现相同的错误。
答案 0 :(得分:0)
您是否尝试使用普通按钮而不是imageButton? 像:
<Button
android:background="@drawable/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />