流失布局

public class Myview extends ViewGroup {
    public Myview(Context context) {
        this(context,null);
    }

    public Myview(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //测量模式
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        //测量ViewGroup的宽高
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        //ViewGroup的padding
        int paddingTop = getPaddingTop();
        int paddingBottom = getPaddingBottom();
        int paddingLeft = getPaddingLeft();
        int paddingRight = getPaddingRight();
        //当前宽度
        int curWidth = 0;
        //当前高度
        int curHeight = paddingTop + paddingBottom;
        //最大宽度
        int widthMax = 0;
        for(int i = 0;i <  getChildCount();){
            View childView = getChildAt(i);
            measureChild(childView, widthMeasureSpec, heightMeasureSpec);
            MarginLayoutParams marginLayoutParams = (MarginLayoutParams) childView.getLayoutParams();
            int childWidth = childView.getMeasuredWidth();
            int childHeight = childView.getMeasuredHeight();
            if(curWidth == 0 ){
                curWidth = paddingLeft + paddingRight;
                curHeight += marginLayoutParams.topMargin + marginLayoutParams.bottomMargin + childHeight;
            }
            if(curWidth + marginLayoutParams.rightMargin + marginLayoutParams.leftMargin + childWidth <= widthSize){
                //不换行
                curWidth += marginLayoutParams.rightMargin + marginLayoutParams.leftMargin + childWidth;
                i++;
            }else{
                //换行
                widthMax = Math.max(widthMax, curWidth);
                curWidth = 0;
            }
            //判断是否是最后一行
            if(i == getChildCount()){
                widthMax = Math.max(widthMax, curWidth);
                curHeight += marginLayoutParams.topMargin + marginLayoutParams.bottomMargin + childHeight;
            }
        }
        setMeasuredDimension(widthMode == MeasureSpec.EXACTLY ? widthSize : widthMax ,
                heightMode == MeasureSpec.EXACTLY  ? heightSize : curHeight);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        //获取ViewGroup的padding
        int paddingTop = getPaddingTop();
        int paddingBottom = getPaddingBottom();
        int paddingLeft = getPaddingLeft();
        int paddingRight = getPaddingRight();
        //当前宽度;
        int curWidth = paddingLeft;
        //当前高度
        int curHeight = paddingTop;
        for(int i = 0; i < getChildCount();){
            View childView = getChildAt(i);
            MarginLayoutParams marginLayoutParams = (MarginLayoutParams) childView.getLayoutParams();
            int childWidth = childView.getMeasuredWidth();
            int childHeight = childView.getMeasuredHeight();
            if(curWidth + paddingRight + marginLayoutParams.rightMargin + marginLayoutParams.leftMargin + childWidth <= getWidth()){
                //不换行
                childView.layout(curWidth + marginLayoutParams.leftMargin,
                        curHeight + marginLayoutParams.topMargin,
                        curWidth + marginLayoutParams.leftMargin + childWidth,
                        curHeight + marginLayoutParams.topMargin + childHeight);
                curWidth += marginLayoutParams.leftMargin + childWidth + marginLayoutParams.rightMargin;
                i ++;
            }else{
                //换行
                curHeight += marginLayoutParams.topMargin + childHeight + marginLayoutParams.bottomMargin;
                curWidth = paddingLeft;
            }
        }

    }
}
//MainActivity里面的方法
private String mNames[] = { "完美世界", "大主宰", "斗破苍穹", "绝世唐门", "你好啊", "遮天", "微微一笑很倾城", "何以笙箫默", "三生三世十里桃花", "琅琊榜",
        "天下", "刀剑神皇", "大漠谣", "回到明朝当王爷", "兵临天下", "谁的青春不迷茫" };


private void initChildViews() {
    Myview mv=findViewById(R.id.mv);
    ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);

    //设置两个控件之间的距离
    lp.leftMargin = 10;
    lp.rightMargin = 10;
    lp.topMargin = 10;
    lp.bottomMargin = 10;
    for (int i = 0; i < mNames.length; i++) {
        view = new TextView(this);
        view.setText(mNames[i]);
       view.setTextColor(Color.WHITE);
       view.setBackgroundDrawable(getResources().getDrawable(R.drawable.textshap));
        mv.addView(view, lp);
        final int finalI = i;
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,mNames[finalI],Toast.LENGTH_SHORT).show();
            }
        });
    }


}
//textshap文件的代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#666666" />

    <padding
        android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp"
        />

</shape>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值