繁体   English   中英

TypedArray getColor始终返回defValue

[英]TypedArray getColor always returns defValue

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

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);

主XML

<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

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

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

这是一个老线程,但没有人回应。 我会的,所以观众可以得到一个答案,我认为你只需要改变你所有的accordion: app:就像这样

app:text_color="@color/text_color"
app:buttonColorPressed="@color/button_pressed"
app:buttonColorUnpressed="@color/button_not_pressed"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM