android – tag需要’drawable’属性

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

内容简介:翻译自: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


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

查看所有标签

猜你喜欢:

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

大数据大创新:阿里巴巴云上数据中台之道

大数据大创新:阿里巴巴云上数据中台之道

邓中华 / 电子工业出版社 / 2018-11 / 99

阿里巴巴云上数据中台正服务着阿里生态中的数十个业务板块、百余家公司、千万级客户,在帮助决策层看清甚至决定业态走向的同时,在上万个业务场景中应用并催生创新。 《大数据大创新:阿里巴巴云上数据中台之道》基于作者在阿里巴巴的十年大数据从业经历,精彩演绎云上数据中台之道。《大数据大创新:阿里巴巴云上数据中台之道》基于大数据探索的大趋势,讲述阿里巴巴云上数据中台顶层设计,再以实际案例详述阿里巴巴云上数......一起来看看 《大数据大创新:阿里巴巴云上数据中台之道》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器