首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypedArray getColor始终返回defValue

TypedArray getColor始终返回defValue
EN

Stack Overflow用户
提问于 2016-03-24 22:27:12
回答 1查看 1.3K关注 0票数 3

在我的XML元素中,我尝试传递来自colors.xml资源文件的颜色。getColor函数总是返回defValue,即使它应该返回colors.xml中给定的颜色。

代码

代码语言:javascript
复制
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.accordion);
final int col_pressed = a.getColor(R.styleable.accordion_buttonColorPressed, Color.BLACK);
final int col_unpressed = a.getColor(R.styleable.accordion_buttonColorPressed, Color.YELLOW);
btn.setBackgroundColor(col_unpressed);

main XML

代码语言:javascript
复制
<mika.actual.AccordionWidget
    xmlns:accordion="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    accordion:text_color="@color/text_color"
    accordion:buttonColorPressed="@color/button_pressed"
    accordion:buttonColorUnpressed="@color/button_not_pressed"
    android:background="#FFFFFFFF"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"> ... </mika.actual.AccordionWidget>

colors.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>

    <color name="text_color">#FFFFFF</color>
    <color name="button_pressed">#666666</color>
    <color name="button_not_pressed">#BBBBBB</color>
</resources>

attrs.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="accordion">
        <attr name="text_color" format="color"/>
        <attr name="buttonColorPressed" format="color"/>
        <attr name="buttonColorUnpressed" format="color"/>
    </declare-styleable>
</resources>
EN

回答 1

Stack Overflow用户

发布于 2019-03-12 23:47:37

这是一个旧的线程,但是没有人响应。我会的,这样观众就可以有答案了我想你只需要把你所有的accordion:改成app:就行了

代码语言:javascript
复制
app:text_color="@color/text_color"
app:buttonColorPressed="@color/button_pressed"
app:buttonColorUnpressed="@color/button_not_pressed"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36202571

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档