流式标签

先上图片看效果
这里写图片描述

思路很简单,就是垂直的LinearLayout里面添加水平的LinearLayout,然后再往水平的LinearLayout里面添加tag

/**
 * Created by star on 2016/9/9
 * 功能:流式标签
 */
public class UserTagsView extends LinearLayout {
    //每一行的LinearLayout
    private LinearLayout linearRow;
    private LinearLayout.LayoutParams rowParams;

    //标签的
    private LinearLayout.LayoutParams textParams;

    private int wholeWidth;
    private int toatlWidth = 0;

    private Context context;

    private float density;

    public UserTagsView(Context context) {
        super(context);
        init(context);
    }

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

    public UserTagsView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        this.context = context;
        rowParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        textParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        textParams.setMargins(5, 5, 5, 5);

        density = context.getResources().getDisplayMetrics().density;
    }

    private int totalMargin;
    public void setTotalMargin(int num){
        this.totalMargin = num;
    }

    //设置tag列表
    public void setTagList(List<UserTag> tagList) {
        /*有时候如果界面没有绘制出来的时候,宽度得到的是0,
     所以要计算一下控件的真实宽度(其实我并不知道怎么利用MeasureSpec去计算,我试了,都不对,求大神赐教。。。),
    为了计算真实宽度,需要把距离两边的距离给传过来,所以就有了上面的方法setTotalMargin(),然后用屏幕宽度去减就好了,
    我们是加载完数据之后才设置的tagList,所以上图中去过的地方和个性标签在加载完数据之后,控件宽度已经得到,这个没什么,
    但是兴趣里面我是一个完全撑开的ListView,因为最外面是ScrollView,两者嵌套就要这样做,可是在适配器的getView()
返回convertView之前,设置了tagList,但是此时控件的宽度还不能得到,所以就要提前计算一下。*/
        if (wholeWidth <= 0) {
            wholeWidth = (int) (MyApplication.getScreenSize().width() - totalMargin * density);
        }
        removeAllViews();
        if (Assert.isValidCollection(tagList)) {
            for (int i = 0; i < tagList.size(); i++) {
                UserTag userTag = tagList.get(i);
                TextView tag = new TextView(context);
                tag.setTextSize(13);
                tag.setBackgroundResource(R.drawable.lv_shape);
                tag.setText(userTag.getName());
                tag.setSingleLine();
                tag.setTextColor(Color.parseColor(userTag.getColor()));
                GradientDrawable background = (GradientDrawable) tag.getBackground();
                background.setColor(Color.parseColor(userTag.getBgColor()));

                int width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                int height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                tag.measure(width, height);
                int tempWidth = tag.getMeasuredWidth() + 10;
                if (i == 0 || toatlWidth + tempWidth > wholeWidth) {
                    toatlWidth = 0;
                    linearRow = new LinearLayout(context);
                    linearRow.setOrientation(LinearLayout.HORIZONTAL);
                    this.addView(linearRow, rowParams);
                }
                linearRow.addView(tag, textParams);
                toatlWidth += tempWidth;
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        wholeWidth = getMeasuredWidth();
    }
}


//这是lv_shape的drawable代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="5dp"/>
    <padding
        android:bottom="2dp"
        android:left="8dp"
        android:right="8dp"
        android:top="2dp"
        />
</shape>

//完全撑开的ListView
public class MyListView extends ListView{
    public MyListView(Context context) {
        super(context);
    }
    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_33666539/article/details/52624224