兴趣树

自定义view

public class CustomTreeView extends LinearLayout {
    private Paint mPaint;

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

    private void init() {
        setWillNotDraw(false);
        mPaint = new Paint();
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(3);
    }

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

        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        int childCount = getChildCount();
        int bottom = 0;
        for (int i = 0; i < childCount; i++) {
            View view = getChildAt(i);
            bottom += view.getMeasuredHeight();
        }

        setMeasuredDimension(sizeWidth, bottom);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        int left = 0, top = 0;

        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View view = getChildAt(i);

            if (i % 2 == 0) {
                left = 0;
            } else {
                left = getMeasuredWidth() / 2;
            }

            view.layout(left, top, left + getMeasuredWidth() / 2, top + view.getMeasuredHeight());
            top += view.getMeasuredHeight();
        }
    }

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

        canvas.drawLine(getMeasuredWidth() / 2, 0, getMeasuredWidth() / 2, getMeasuredHeight(), mPaint);
    }
}

主页面

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    private void initView() {
        final CustomTreeView customTreeView = findViewById(R.id.cfl_search);
        final EditText editText = findViewById(R.id.edit_search);
        findViewById(R.id.iv_search).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView tv = new TextView(MainActivity.this);
                tv.setTextColor(Color.RED);
                tv.setText(editText.getText());
                tv.setBackgroundResource(R.drawable.search_history_bg);
                customTreeView.addView(tv);
                customTreeView.invalidate();
            }
        });
    }
}

猜你喜欢

转载自blog.csdn.net/xieyu1999/article/details/85156236
今日推荐