android – tag需要’drawable’属性

栏目: IOS · Android · 发布时间: 6年前

内容简介:翻译自:https://stackoverflow.com/questions/18913822/item-tag-requires-a-drawable-attribute

我试图非常简单地设计一个Button.我只是希望它在没有按下时带有文本蓝色,而在单击时带有蓝色文本.

我尝试用样式和选择器来做到这一点.

在我的布局中我有这个按钮:

<Button
    android:id="@+id/button1"
    style="@style/MyButton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="@string/login" />

在res / values / styles中我有这个styles.xml:

<style name="MyButton">
    <item name="android:background">@drawable/btn_background</item>
    <item name="android:textColor">@drawable/btn_textcolor</item>
</style>

当然,两个选择器,在res / drawable中,btn_background.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="@color/white" />
   <item android:color="@color/SapphireBlue" />
</selector>

和btn_textcolor.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="@color/SapphireBlue" />
    <item android:color="@color/white" />
</selector>

我运行应用程序或打开布局编辑器时出现的错误是:

<item> tag requires a ‘drawable’ attribute

我理解这个消息,但我没有一个可绘制的,它是一个简单的扁平按钮.

我怎样才能创建这么简单的按钮?

根据 this post 更新,它应该工作.

试试这种方式,希望这可以帮助您解决问题.

btn_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/white" android:state_pressed="true"></item>
    <item android:drawable="@drawable/white" android:state_focused="true"></item>
    <item android:drawable="@drawable/SapphireBlue" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
    <item android:drawable="@drawable/white" android:state_enabled="false"></item>
</selector>

btn_textcolor.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/SapphireBlue" android:state_pressed="true"></item>
<item android:color="@drawable/SapphireBlue" android:state_focused="true"></item>
<item android:color="@drawable/white" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
<item android:color="@drawable/SapphireBlue" android:state_enabled="false"></item>
</selector>

翻译自:https://stackoverflow.com/questions/18913822/item-tag-requires-a-drawable-attribute


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

互联网的基因

互联网的基因

人民邮电出版社 / 2016-9-21 / 48.00元

《互联网的基因》是一本从电信看互联网创新,从互联网看电信创新的力作。作者何宝宏博士长期在电信行业从事互联网领域研究,是极为少有的“既懂IP又懂电信”的专家。该书借以电信和互联网技术创新的大脉络,用轻松、诙谐、幽默的语言,结合经济学、社会学、哲学、人类学甚至心理学理论,揭示互联网、云计算、大数据以及目前最热门的区块链等技术发展背后的规律。作者在该书中明确表示,互联网是新的技术物种,互联网有基因,互联......一起来看看 《互联网的基因》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

MD5 加密
MD5 加密

MD5 加密工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具