TypedArray .getColor()始终在自定义视图中返回-1

时间:2015-05-25 01:09:22

标签: java android android-layout

我创建了一个名为TextView的子类TextViewEx的自定义视图。此视图通过xml为Drawables提供的复合TextView增加了更多的灵活性。我想要的部分功能是能够对复合drawable进行着色,但无论出于何种原因,返回的颜色始终为-1。这是代码:

attrs.xml:

<resources>
    <declare-styleable name="TextViewEx">
        <attr name="drawableLeftWidth" format="dimension" />
        <attr name="drawableLeftHeight" format="dimension" />
        <attr name="drawableLeftTint" format="color" />

        <attr name="drawableTopWidth" format="dimension" />
        <attr name="drawableTopHeight" format="dimension" />
        <attr name="drawableTopTint" format="color" />

        <attr name="drawableRightWidth" format="dimension" />
        <attr name="drawableRightHeight" format="dimension" />
        <attr name="drawableRightTint" format="color" />

        <attr name="drawableBottomWidth" format="dimension" />
        <attr name="drawableBottomHeight" format="dimension" />
        <attr name="drawableBottomTint" format="color" />
    </declare-styleable>
</resources>

TextViewEx.java:

public class TextViewEx
        extends TextView {

    public TextViewEx(Context context) {
        super(context);
        init(null, 0);
    }

    public TextViewEx(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public TextViewEx(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }

    private void init(AttributeSet attrs, int defStyle) {

        if (attrs==null) {
            return;
        }

        TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.TextViewEx, defStyle, 0);

        int lTint = -1, lWidth = -1, lHeight = -1;
        int tTint = -1, tWidth = -1, tHeight = -1;
        int rTint = -1, rWidth = -1, rHeight = -1;
        int bTint = -1, bWidth = -1, bHeight = -1;

        try {
            lTint = a.getColor(R.styleable.TextViewEx_drawableLeftTint, getCurrentTextColor());
            lWidth = a.getDimensionPixelSize(R.styleable.TextViewEx_drawableLeftWidth, 0);
            lHeight = a.getDimensionPixelSize(R.styleable.TextViewEx_drawableLeftHeight, 0);

            tTint = a.getColor(R.styleable.TextViewEx_drawableTopTint, getCurrentTextColor());
            tWidth = a.getDimensionPixelSize(R.styleable.TextViewEx_drawableTopWidth, 0);
            tHeight = a.getDimensionPixelSize(R.styleable.TextViewEx_drawableTopHeight, 0);

            rTint = a.getColor(R.styleable.TextViewEx_drawableRightTint, getCurrentTextColor());
            rWidth = a.getDimensionPixelSize(R.styleable.TextViewEx_drawableRightWidth, 0);
            rHeight = a.getDimensionPixelSize(R.styleable.TextViewEx_drawableRightHeight, 0);

            bTint = a.getColor(R.styleable.TextViewEx_drawableBottomTint, getCurrentTextColor());
            bWidth = a.getDimensionPixelSize(R.styleable.TextViewEx_drawableBottomWidth, 0);
            bHeight = a.getDimensionPixelSize(R.styleable.TextViewEx_drawableBottomHeight, 0);
        } finally {
            a.recycle();
        }

        Drawable[] drawables = getCompoundDrawables();
        if (drawables[0]!=null && lWidth!=0 && lHeight!=0) {
            drawables[0].setBounds(0, 0, lWidth, lHeight);
        }
        if (drawables[1]!=null && tWidth!=0 && tHeight!=0) {
            drawables[1].setBounds(0, 0, tWidth, tHeight);
        }
        if (drawables[2]!=null && rWidth!=0 && rHeight!=0) {
            drawables[2].setBounds(0, 0, rWidth, rHeight);
        }
        if (drawables[3]!=null && bWidth!=0 && bHeight!=0) {
            drawables[3].setBounds(0, 0, bWidth, bHeight);
        }

        if (drawables[0]!=null && lTint!=-1) {
            Graphics.tint(drawables[0], lTint);
        }
        if (drawables[1]!=null && tTint!=-1) {
            Graphics.tint(drawables[1], tTint);
        }
        if (drawables[2]!=null && rTint!=-1) {
            Graphics.tint(drawables[2], rTint);
        }
        if (drawables[3]!=null && bTint!=-1) {
            Graphics.tint(drawables[3], bTint);
        }

        setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3]);

    }
}

并在使用中:

    <com.cheerfulinc.flipagram.view.TextViewEx
        android:id="@+id/likesCount"
        android:gravity="center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="30dp"
        android:layout_marginBottom="30dp"
        android:background="@drawable/red_pill_text"
        android:textColor="@android:color/white"
        android:text="22,4456"
        android:drawableRight="@drawable/fg_icon_heart"
        android:drawablePadding="6dp"
        app:drawableRightWidth="12dp"
        app:drawableRightHeight="12dp"
        app:drawableRightTint="#FFFFFFFF" />

我也尝试过:app:drawableRightTint="@android:color/white"但是没有问题,返回的颜色值总是-1

2 个答案:

答案 0 :(得分:9)

我使用的是白色,白色是-1 :)上面的代码运行得很好。我为-1切换了Integer.MAX_VALUE,一切都很好。

答案 1 :(得分:1)

使用getColorStateList代替getColor