<item> tag requires a 'drawable' attribute or child tag defining a drawab
来源:互联网 发布:淘宝一键复制在哪里 编辑:程序博客网 时间:2024/05/16 10:16
异常日志:
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #3: <item> tag requires a 'drawable' attribute or child tag defining a drawable
产生原因:
我的一个button按钮的background属性中设置成"@color/button_text_selector",按照异常来说,这个background这个属性的值必须是drawable类型的,不能是color类型。
drawable&color:
本来我是想给button设置一个selector,让按钮背景正常状态下是一个颜色值,点击状态下背景又是另外一个值,但是没想到报了这样一个错误,既然background只能设置为drawable,那我就写一个drawable的selector给button设置不同状态下的颜色值,如下所示:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 点击状态下的效果 --> <item android:state_pressed="true"> <shape> <!-- 设置背景填充色为黑色 --> <solid android:color="@color/black"/> <!-- 设置边框宽度为1dp,边框颜色为白色 --> <stroke android:width="1dp" android:color="@color/white" /> <!-- 设置按钮圆角半径为5dp --> <corners android:radius="5dp" /> <!-- 设置按钮中间文字距上下左右都为10dp的间距 --> <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" /> </shape> </item> <!-- 正常状态下(非点击)的效果 --> <item android:state_pressed="false"> <shape> <!-- 设置背景填充色为白色 --> <solid android:color="@color/white"/> <!-- 设置边框宽度为1dp,边框颜色为黑色 --> <stroke android:width="1dp" android:color="@color/black" /> <!-- 设置按钮圆角半径为5dp --> <corners android:radius="5dp" /> <!-- 设置按钮中间文字距上下左右都为10dp的间距 --> <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" /> </shape> </item></selector>
这样即可对不同状态下的button设置颜色值了,还可以设置其他圆角之类的效果;当然这个只能设置按钮背景的效果,但是文字是没有点击效果的,如果使用我上面缩写的selector,那么就会在按下按钮的时候出现这个效果:
在按钮被点击的时候背景色和字体颜色混成一体,所以我们最好同时给字体颜色设置一个selector,如下:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- button点击状态下的字体颜色 --> <item android:state_pressed="true" android:color="@color/white"></item> <!-- button正常状态下的字体颜色 --> <item android:state_pressed="false" android:color="@color/black"></item></selector>
最后在界面layout中设置button属性:
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" android:background="@drawable/button_selector" android:textColor="@color/button_text_selector"/>
这样我们的button效果就会如图:
button正常状态下为白底黑字黑边,点击状态下为黑底白字白边。
demo下载:
http://download.csdn.net/detail/zhufuing/6974387
4 0
- <item> tag requires a 'drawable' attribute or child tag defining a drawab
- <item> tag requires a 'drawable' attribute or child tag defining a drawab
- textcolor 设置selecter 报错: <item> tag requires a 'color' attribute or child tag defining a drawab
- <item> tag requires a 'color' attribute or child tag defining a drawable
- android背景设置报错 <item> tag requires a 'drawable' attribute or child tag defining a drawable
- android背景设置报错 <item> tag requires a 'drawable' attribute or child tag defining a drawable<转>
- Binary XML file line #4: <item> tag requires a 'drawable' attribute or child tag defining a drawable
- android背景设置报错 <item> tag requires a 'drawable' attribute or child tag defining a drawable
- android背景设置报错 <item> tag requires a 'drawable' attribute or child tag defining a drawable
- Binary XML file line #3: <item> tag requires a 'drawable' attribute or child tag defining a drawable
- android背景设置报错 <item> tag requires a 'drawable' attribute or child tag defining a drawable
- tag requires a 'drawable' attribute or child tag defining a drawable 错误解决
- AndroidStudio-xml预览布局无法正常显示tag requires a 'drawable' attribute or child tag defining a drawable
- "<item> tag requires a 'drawable' attribute"-selector设置颜色不成功-解决方案
- "<item> tag requires a 'drawable' attribute"-selector设置颜色不成功-解决方案
- "<item> tag requires a 'drawable' attribute"-selector设置颜色不成功-解决方案
- <item> tag requires a 'drawable' attribute"-selector设置颜色报错-原因及解决方案
- <item> tag requires a 'drawable' attribute"-selector设置颜色不成功-解决方案
- 关于spring定时器的搭建
- 运行数据库遇到错误oracle not aviliable解决过程
- Fortran common
- B. Bear and Strings
- 在.net中悄悄执行dos命令,并获取执行的结果
- <item> tag requires a 'drawable' attribute or child tag defining a drawab
- 自定义横向ProgressBar样式圆角改直角
- lua随机数
- plsql连接数据库的方法
- IOS手势初步学习--监听相应的手势处理
- Oracle 10g RAC 客户端连接不稳定
- Ogre材质系统总结(一)
- 发布一个web项目到Internet?
- Combiner执行顺序引起的错误,无法产生结果