项目中使用到了标签,直接看效果图
标签可能多个,可能长度不一。自定义流式布局实现该效果。
自定义的MyFlowLayout.java如下:
public class MyFlowLayout extends ViewGroup {
private List<Row> mRows = new ArrayList<>();
private int mHorizontalChildGap;
private int mVerticalChildGap;
private boolean mIsDistributionWhiteSpacing = true;
private int mLabelTextColor = Color.BLACK;
private int mLabelBackgroundColor = Color.WHITE;
private float mLabelTextSize = 10;
private float mLabelPadding = 0;
private int mLabelBackgroundDrawable = R.drawable.lable_drawable;
public MyFlowLayout(Context context) {
this(context, null);
}
public MyFlowLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyFlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initDefaultAttrs(context);
initCustomAttrs(context, attrs);
}
private void initDefaultAttrs(Context context) {
mHorizontalChildGap = dp2px(context, 10);
mVerticalChildGap = dp2px(context, 10);
}
private void initCustomAttrs(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyFlowLayout);
final int N = typedArray.getIndexCount();
for (int i = 0; i < N; i++) {
initCustomAttr(typedArray.getIndex(i), typedArray);
}
typedArray.recycle();
}
private void initCustomAttr(int attr, TypedArray typedArray) {
/**
* getDimension和getDimensionPixelOffset的功能差不多,都是获取某个dimen的值,如果是dp或sp的单位,将其乘以density,如果是px,则不乘;两个函数的区别是一个返回float,一个返回int. getDimensionPixelSize则不管写的是dp还是sp还是px,都会乘以denstiy.
*/
switch (attr) {
case R.styleable.MyFlowLayout_fl_verticalChildGap://标签之间的垂直间距
mHorizontalChildGap = typedArray.getDimensionPixelOffset(attr, mVerticalChildGap);
break;
case R.styleable.MyFlowLayout_fl_horizontalChildGap://标签之间的水平间距
mVerticalChildGap = typedArray.getDimensionPixelOffset(attr, mHorizontalChildGap);
break;
case R.styleable.MyFlowLayout_fl_isDistributionWhiteSpacing://是否平均分配每一行的剩余水平方向的空白区域给该行的标签
mIsDistributionWhiteSpacing = typedArray.getBoolean(attr, mIsDistributionWhiteSpacing);
break;
case R.styleable.MyFlowLayout_fl_LabelTextColor://标签字体颜色
mLabelTextColor = typedArray.getColor(attr, mLabelTextColor);
break;
case R.styleable.MyFlowLayout_fl_LabelBackgroundColor://标签背景颜色
mLabelBackgroundColor = typedArray.getColor(attr, mLabelBackgroundColor);
break;
case R.styleable.MyFlowLayout_fl_LabelTextSize://标签字体大小
mLabelTextSize = typedArray.getFloat(attr, mLabelTextSize);
break;
case R.styleable.MyFlowLayout_fl_LabelPadding://标签内边距
mLabelPadding = typedArray.getFloat(attr, mLabelPadding);
break;
case R.styleable.MyFlowLayout_fl_LabelBackgroundDrawable://标签内边距
mLabelBackgroundDrawable = typedArray.getResourceId(attr, mLabelBackgroundDrawable);
break;
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
/**
* 1.EXACTLY:100dp,match_parent
* 2.AT_MOST:wrap_content
* 3.UNSPCIFIED:子控件想要多大就多大,很少见(ScrollView)
*/
int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
mRows.clear();
Row row = new Row(sizeWidth);
View child;
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
child = getChildAt(i);
measureChild(child, widthMeasureSpec, heightMeasureSpec);
if (!row.addChild(child)) {
mRows.add(row);
row = new Row(sizeWidth);
row.addChild(child);
}
}
// 添加最后一行
if (!mRows.contains(row)) {
mRows.add(row);
}
int height = 0;
int rowCount = mRows.size();
for (int i = 0; i < rowCount; i++) {
height += mRows.get(i).mHeight;
if (i != rowCount - 1) {
height += mVerticalChildGap;
}
}
setMeasuredDimension(sizeWidth, modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height + getPaddingTop() + getPaddingBottom());
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int rowCount = mRows.size();
int top = getPaddingTop();
Row row;
for (int i = 0; i < rowCount; i++) {
row = mRows.get(i);
if (mIsDistributionWhiteSpacing && i != rowCount - 1) {
row.layout(true, top);
} else {
row.layout(false, top);
}
top += row.mHeight + mVerticalChildGap;
}
}
//设置标签(标签内容是数据集合)
public void addView(List<String> list) {
removeAllViews();
if (list != null)
for (String string : list) {
super.addView(getLabel(string));
}
}
//设置标签(标签内容是以","作为分割符的)
public void addView(String lable) {
removeAllViews();
if (!TextUtils.isEmpty(lable)) {
String[] lableList = lable.split(",");
if (lableList != null){
for (String string : lableList) {
super.addView(getLabel(string));
}
}
}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private TextView getLabel(String text) {
TextView label = new TextView(getContext());
label.setTextColor(mLabelTextColor);
// label.setBackgroundColor(mLabelBackgroundColor);
label.setGravity(Gravity.CENTER);
label.setSingleLine(true);
label.setTextSize(mLabelTextSize);
label.setEllipsize(TextUtils.TruncateAt.END);
label.setBackground(getResources().getDrawable(mLabelBackgroundDrawable));
int padding = MyFlowLayout.dp2px(getContext(), mLabelPadding);
label.setPadding(padding, 0, padding, 0);
label.setText(text);
return label;
}
private class Row {
private List<View> mViews = new ArrayList<>();
private int mWidth;
private int mNewWidth;
private int mHeight;
private int mMaxWidth;
public Row(int sizeWidth) {
mMaxWidth = sizeWidth - getPaddingLeft() - getPaddingRight();
}
public boolean addChild(View child) {
if (isOutOfMaxWidth(child.getMeasuredWidth())) {
return false;
} else {
mViews.add(child);
mWidth = mNewWidth;
int childHeight = child.getMeasuredHeight();
mHeight = mHeight < childHeight ? childHeight : mHeight;
return true;
}
}
private boolean isOutOfMaxWidth(int childWidth) {
if (mViews.size() == 0) {
mNewWidth = mWidth + childWidth;
} else {
mNewWidth = mWidth + mHorizontalChildGap + childWidth;
}
return mNewWidth > mMaxWidth;
}
/**
* @param isNeedSplit 是否需要平均分配空白区域给每一个子孩子
* @param top
*/
public void layout(boolean isNeedSplit, int top) {
if (mViews.size() == 0) {
return;
}
int left = getPaddingLeft();
int count = mViews.size();
int splitWidth = (mMaxWidth - mWidth) / count;
View view;
for (int i = 0; i < count; i++) {
view = mViews.get(i);
int childWidth = view.getMeasuredWidth();
int childHeight = view.getMeasuredHeight();
if (isNeedSplit) {
childWidth = childWidth + splitWidth;
view.getLayoutParams().width = childWidth;
if (splitWidth > 0) {
/**
* 1.EXACTLY:100dp,match_parent
* 2.AT_MOST:wrap_content
* 3.UNSPCIFIED:子控件想要多大就多大,很少见(ScrollView)
*/
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY);
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY);
view.measure(widthMeasureSpec, heightMeasureSpec);
}
}
int topOffset = (int) ((mHeight - childHeight) / 2.0 + 0.5);
view.layout(left, top + topOffset, left + childWidth, top + topOffset + childHeight);
left += childWidth + mHorizontalChildGap;
}
}
}
public static int dp2px(Context context, float dpValue) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, context.getResources().getDisplayMetrics());
}
}
然后在布局中使用该控件,代码中调用addView方法即可。