Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

label背景色动态色值渲染异常 #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions labels/src/main/java/com/donkingliang/labels/LabelsView.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
Expand All @@ -25,6 +24,7 @@ public class LabelsView extends ViewGroup implements View.OnClickListener, View.
private ColorStateList mTextColor;
private float mTextSize;
private Drawable mLabelBg;
private int labelBgId;
private int mLabelWidth = -2;
private int mLabelHeight = -2;
private int mLabelGravity = Gravity.CENTER;
Expand Down Expand Up @@ -159,12 +159,14 @@ private void getAttrs(Context context, AttributeSet attrs) {
mWordMargin = mTypedArray.getDimensionPixelOffset(R.styleable.LabelsView_wordMargin, dp2px(5));
if (mTypedArray.hasValue(R.styleable.LabelsView_labelBackground)) {
int labelBgResId = mTypedArray.getResourceId(R.styleable.LabelsView_labelBackground, 0);
if (labelBgResId != 0) {
mLabelBg = getResources().getDrawable(labelBgResId);
} else {
int labelBgColor = mTypedArray.getColor(R.styleable.LabelsView_labelBackground, Color.TRANSPARENT);
mLabelBg = new ColorDrawable(labelBgColor);
}
labelBgId = labelBgResId;
// if (labelBgResId != 0) {
//
// mLabelBg = getResources().getDrawable(labelBgResId);
// } else {
// int labelBgColor = mTypedArray.getColor(R.styleable.LabelsView_labelBackground, Color.TRANSPARENT);
// mLabelBg = new ColorDrawable(labelBgColor);
// }
} else {
mLabelBg = getResources().getDrawable(R.drawable.default_label_bg);
}
Expand Down Expand Up @@ -554,7 +556,11 @@ private <T> void addLabel(T data, int position, LabelTextProvider<T> provider) {
label.setTextColor(mTextColor);
//设置给label的背景(Drawable)是一个Drawable对象的拷贝,
// 因为如果所有的标签都共用一个Drawable对象,会引起背景错乱。
label.setBackgroundDrawable(mLabelBg.getConstantState().newDrawable());
if (labelBgId > 0) {
label.setBackgroundResource(labelBgId);
} else {
label.setBackgroundDrawable(mLabelBg.getConstantState().newDrawable());
}
//label通过tag保存自己的数据(data)和位置(position)
label.setTag(KEY_DATA, data);
label.setTag(KEY_POSITION, position);
Expand Down Expand Up @@ -811,7 +817,9 @@ public <T> List<T> getSelectLabelDatas() {
* @param resId
*/
public void setLabelBackgroundResource(int resId) {
setLabelBackgroundDrawable(getResources().getDrawable(resId));
labelBgId = resId;
mLabelBg = null;
setLabelBackgroundDrawable();
}

/**
Expand All @@ -820,20 +828,23 @@ public void setLabelBackgroundResource(int resId) {
* @param color
*/
public void setLabelBackgroundColor(int color) {
setLabelBackgroundDrawable(new ColorDrawable(color));
mLabelBg = new ColorDrawable(color);
labelBgId = 0;
setLabelBackgroundDrawable();
}

/**
* 设置标签背景
*
* @param drawable
*/
public void setLabelBackgroundDrawable(Drawable drawable) {
mLabelBg = drawable;
private void setLabelBackgroundDrawable() {
int count = getChildCount();
for (int i = 0; i < count; i++) {
TextView label = (TextView) getChildAt(i);
label.setBackgroundDrawable(mLabelBg.getConstantState().newDrawable());
if (labelBgId > 0) {
label.setBackgroundResource(labelBgId);
} else {
label.setBackgroundDrawable(mLabelBg.getConstantState().newDrawable());
}
}
}

Expand Down