您的位置:首页 > 其它

Binary XML file line : Error inflating class <unknown> 解决

2013-05-03 16:55 260 查看
思路:1.官方文档

2.百度、谷歌

情况解问题:今天在捣鼓了button,我想做个这样的效果:不同的state来触发不同的按钮背景颜色。我当时为了测试就简单写了个:

新建res/drawable/selector.xml:

selector.xml

<?xml

version="1.0"

encoding="utf-8"

?> 

<selector

xmlns:android="http://schemas.android.com/apk/res/android"> 

<item

      android:state_pressed="true"

      android:color="#3197FF"/>

</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">

	<Button android:id="@+id/button3"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="这是button" 
		android:background="@drawable/selector"/>

</LinearLayout>


运行项目出现错误:<item> tag requires a 'drawable' attribute or child tag defining a drawable 大概的意思就是:item标签需要一个drawable属性或者一个子标签来定义一个drawable属性。

于是我去官网查看了selector相关信息(http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList),发现关于Button有这样一段代码:

EXAMPLE:
XML file saved at res/drawable/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" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/button_focused" /> <!-- focused -->
    <item android:state_hovered="true"
          android:drawable="@drawable/button_focused" /> <!-- hovered -->
    <item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>
This layout XML applies the state list drawable to a Button:

<Button
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:background="@drawable/button" />


发现 android:drawable="@drawable/button_pressed" 这行,但是还是一头雾水,好吧,我直接百度问题,找到这个问题解决方案http://www.360doc.com/content/13/0503/11/12091541_282627062.shtml,自己看了下。

解决方案一:

最后这样修改就出效果了

selector.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/juhuangse"/> 
</selector>


新建res/value/colors.xml:

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    
    <drawable name="juhuangse">#EE9A00</drawable>
  
</resources>


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">

	<Button android:id="@+id/button3"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="这是button" 
		android:background="@drawable/selector"/>

</LinearLayout>


运行项目,效果OK了,这里没贴些效果图。

为什么这样修改就可以呢?大概原因正如上面一篇解决方案里面说的,关于修改背景,你不能使用color 属性的selector,应该使用drawable属性的selector。

解决方案二:

另一种稍微繁琐的解决方案

新建res/drawable/selector.xml:

selector.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/pressed"/> 
    
</selector>


新建res/drawable/pressed.xml:

pressed.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle">
    <solid android:color="@drawable/juhuangse" />
</shape>


main.xml以及colors.xml同第一个解决方案。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐