1. 程式人生 > >流佈局設定寬高形式

流佈局設定寬高形式

package com.bawei.myapplication.weektest;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

public class WeekFlowLayout extends LinearLayout {
    /**
     * 孩子中最高的一個
     */
    private int mChildMaxHeight;

    /**
     * 每一個孩子的左右的間距
     * 20是預設值,單位是px
     */
    private int mHSpace = 20;

    /**
     * 每一行的上下的間距
     * 20是預設值,單位是px
     */
    private int mVSpace = 20;


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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        // 拿到父容器推薦的寬和高以及計算模式
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);

        //測量孩子的大小,一定要寫這個
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        //尋找孩子中最高的一個孩子,找到的值會放在mChildMaxHeight變數中
        findMaxChildMaxHeight();

        //初始化值
        int left = 0, top = 0;

        //迴圈所有的孩子
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View view = getChildAt(i);
            //是否是一行的開頭
            if (left != 0) {
                //需要換行了,因為放不下啦
                if ((left + view.getMeasuredWidth()) > sizeWidth) {
                    //計算出下一行的top
                    top += mChildMaxHeight + mVSpace;
                    left = 0;
                }
            }
            left += view.getMeasuredWidth() + mHSpace;
        }
        setMeasuredDimension(sizeWidth, (top + mChildMaxHeight) > sizeHeight ? sizeHeight : top + mChildMaxHeight);

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        findMaxChildMaxHeight();

        //開始安排孩子的位置

        //初始化值
        int left = 0, top = 0;

        //迴圈所有的孩子
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View view = getChildAt(i);
            //是否是一行的開頭
            if (left != 0) {
                //需要換行了,因為放不下啦
                if ((left + view.getMeasuredWidth()) > getWidth()) {
                    //計算出下一行的top
                    top += mChildMaxHeight + mVSpace;
                    left = 0;
                }
            }

            //安排孩子的位置
            view.layout(left, top, left + view.getMeasuredWidth(), top + mChildMaxHeight);
            left += view.getMeasuredWidth() + mHSpace;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    /**
     * 尋找孩子中最高的一個孩子
     */
    private void findMaxChildMaxHeight() {
        mChildMaxHeight = 0;
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View view = getChildAt(i);
            if (view.getMeasuredHeight() > mChildMaxHeight) {
                mChildMaxHeight = view.getMeasuredHeight();
            }
        }
    }
}

刪除線格式

mainActivity

package com.bawei.myapplication.weektest;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bawei.myapplication.R;

public class WeekTestActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_week_test);

        init();
    }

    private void init(){
        final WeekFlowLayout fl = findViewById(R.id.fl);
        final EditText et = findViewById(R.id.et);
        findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView tv = new TextView(WeekTestActivity.this);
                tv.setText(et.getText());
                tv.setTextColor(Color.RED);
                tv.setBackgroundResource(R.drawable.edit_bg);
                fl.addView(tv);
            }
        });
    }
}