`
modabobo
  • 浏览: 510234 次
文章分类
社区版块
存档分类
最新评论

<item> tag requires a 'drawable' attribute or child tag defining a drawab

 
阅读更多

异常日志:

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

分享到:
评论

相关推荐

    VB编程资源大全(英文源码 网络)

    tlsNet.zip&lt;br&gt;TILISOFT Internet ActiveX Controls - Retrieve HTML pages from the Net - Post data to HTTP server &lt;END&gt;&lt;br&gt;19 , pingmon.zip&lt;br&gt;A ping monitor for the network administrator. Based on API ...

    VB编程资源大全(英文源码 控件)

    on left and right side of a text box.&lt;END&gt;&lt;br&gt;40,Assist.zip&lt;br&gt;A simple application with source code which shows how to save the contents of a rich text box without the help of common dialog box.&lt;END&gt;...

    Using DIB Sections in VB

    &lt;br&gt;&lt;br&gt; m_hDC = CreateCompatibleDC(0) &lt;br&gt; If (m_hDC &lt;&gt; 0) Then &lt;br&gt; If (CreateDIB(m_hDC, lWidth, lHeight, m_hDIb)) Then &lt;br&gt; m_hBmpOld = SelectObject(m_hDC, m_hDIb) &lt;br&gt; Create = True &lt;br&gt; Else &lt;br...

    DIBSection Class VB

    &lt;br&gt;A DIB (Device Independent Bitmap) Section is a GDI object like a standard DIB but which additionally provides a pointer to the memory used to store the bitmap bits to which the creating ...

    tomcat-5.5.23

    ============================================&lt;br&gt;&lt;br&gt;Out of the box, Tomcat 5.5 requires the Java 2 Standard Edition Runtime&lt;br&gt;Environment (JRE) version 5.0 or later. However, you can also run Tomcat...

    Alcohol 120%

    Windows 95 must be OSR2 or later version&lt;br&gt;&lt;br&gt;32MB (or more) of RAM&lt;br&gt;&lt;br&gt;10GB (or more) hard disk is recommended (a 74 minute CD image requires 650-700MB)&lt;br&gt;&lt;br&gt;One or more bus-mastering SCSI ...

    VB编程资源大全(英文源码 DirectX)

    1,CODE_UPLOAD137411132001.zip&lt;br&gt;A 3Dimensional park with lolly pop trees and flying birds. Programmed with Direct3D (Retained Mode). A good program to learn some simple direct3d in. Requires the ...

    network coding

    determined the&lt;br&gt;multicast capacity in a network of lossless links and showed that achieving the multicast&lt;br&gt;capacity requires in general the use of network coding. This thesis presents the ...

    A+ Certification Practice Questions Exam Cram 2

    Brooks , Ed Tittel &lt;br&gt;&lt;br&gt;Paperback: 408 pages &lt;br&gt;Publisher: Que (March 20, 2004) &lt;br&gt;Language: English &lt;br&gt;ISBN-10: 0789731088 &lt;br&gt;ISBN-13: 978-0789731081 &lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;The A+ certification is ...

    visual assist v 10.4.1632 with crack

    (case=9436) &lt;br&gt;STL list&lt;&gt; and vector&lt;&gt; member lists appear correctly following a "using namespace std::list" or "using namespace std::vector" directive. (case=12345) 7226 &lt;br&gt;Empty C++ preprocessor ...

    Visual assist x 10.4.1616 and crack

    Build 1616&lt;br&gt;requires software maintenance through 2007.11.08. (Release candidate.) &lt;br&gt;New! Create VA Snippet from selection. &lt;br&gt;.NET references are now parsed even when 'parse all files' is ...

    Ajax 探密

    &lt;br&gt;&lt;br&gt;A smart collection of 100 insider tips and tricks, Ajax Hacks covers all of the technology's finer points. Want to build next-generation web applications today? This book can show you how. ...

    visual assist 1640

    &lt;br&gt;&lt;br&gt;Build 1640&lt;br&gt;requires software maintenance through 2008.05.22. (General release.) &lt;br&gt;Fixed spurious coloring of string literals in VC6. (case=16454) 7565 &lt;br&gt;Fixed Asian character display ...

    subclipse 1.4.1

    Subclipse 1.4.x requires Subversion 1.5.0 version of JavaHL/SVNKit &lt;br&gt;&lt;br&gt;Links for 1.4.x Release:&lt;br&gt;Changelog: http://subclipse.tigris.org/subclipse_1.4.x/changes.html&lt;br&gt;Eclipse update site URL: ...

    AspxBBS论坛 v2.5 正式版

    有些服务器的MDAC版本还是2.5的,如果MDAC的版本太低的话,会有如下出错信息:&lt;br&gt; The .Net Data OLE DB Provider(System.Data.OleDb) requires Microsoft Data Access Components(MDAC) version 2.6 or later....

    解决Java 调用 Ant 包含 <import> 异常:import requires support in ProjectHelper

    NULL 博文链接:https://yxhcquedu.iteye.com/blog/861110

    MySQL 5.1 帮助文档 chm格式

    MySQL 5.1 帮助文档 chm格式&lt;br&gt;&lt;br&gt;MySQL 5.1 Reference Manual&lt;br&gt;Copyright 1997-2006 MySQL AB &lt;br&gt;&lt;br&gt;This documentation is NOT distributed under a GPL license. Use of this documentation is subject to ...

    Lucene查询工具LQT.zip

    --analyzer &lt;arg&gt; for query, (KeywordAnalyzer | StandardAnalyzer) (defaults to KeywordAnalyzer) --fields &lt;arg&gt; fields to include in output (defaults to all) -i,--index &lt;arg&gt; index (required) --...

Global site tag (gtag.js) - Google Analytics