(1)解决android.view.InflateException: Binary XML file line #10: Error inflating class Button
1、drawable的v21、v24问题:当源文件放在drawable-v21文件夹下,而使用的安卓设备并不是API-v21以上版本,就会出现找不到图片/drawable类型的xml等文件情况。
同理,当value文件夹中有些值有一定的限制,比如 dimens.xml(w820dp)与dimens.xml同时存在,而某一文件只放在了(w820dp)中,就会出现上述错误。
2、网络上导致这个问题的原因还有:从网上下载的demo编码方式是否与本地编码方式相同(Eclipse)。
(2.1)正确设置Button(按下、不可用、正常)时的状态。
在drawable文件夹下 新建 button.xml文件
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/mybutton_press" android:state_pressed="true"></item> <item android:drawable="@drawable/mybutton_disable" android:state_enabled="false"></item> <item android:drawable="@drawable/mybutton" android:state_pressed="false"></item> </selector>
注意:必须将state_pressed="false"的item放在最后。因为机制是按照顺序检查的,如果放在前面,就不会进行后面item的检查,就不会显示。
mybutton_press mybutton_disable mybutton可以是放在drawable中的图片,也可是自定义的xml形状。而且命名不要与该文件相同。(不管后缀相不相同,名字都不能相同)
然后将button的background的背景设置为button.xml。
(2.2)解决 设置的背景没有随着按键改变的问题。
1、上述所说,没有将state_pressed="false"的item放在最后。
2、该button.xml(要用的背景文件)没有同步更新在drawble drawable-v21 drawable-v24中(假如设置了文件夹的话)。
(2.3)通过设置style的方式将使得更改样式更加方便。
在布局文件(Layout下的xml文件)中:
<Button style="@style/Button_style" android:layout_width="wrap_content" android:layout_height="@dimen/layout_height"/>
在value\style文件夹中:
此处有两个styles.xml文件,都要更改,否则就无法在所有类型的安卓机器上显示。
<style name="Button_style"> <item name="android:background">@drawable/button</item> <item name="android:textColor">@color/white</item> </style>