我创建了一个自定义视图,它将TextVIEw的子类称为TextVIEwEx.此视图通过xml为TextVIEw提供的复合Drawable增加了更多的灵活性.我想要的部分功能是能够对复合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
解决方法:
我用白色,白色是-1 :)上面的代码工作得很好.我为Integer.MAX_VALUE切换了-1,一切都很好.
总结以上是内存溢出为你收集整理的java – TypedArray .getColor()始终在自定义视图中返回-1全部内容,希望文章能够帮你解决java – TypedArray .getColor()始终在自定义视图中返回-1所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)