流式布局简单使用

流式布局简单使用

FlowView

protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        //获得控件宽度
        int width = getWidth();
        //定义常量行数
        int row = 0;
        //子控件左边的坐标
        int disWidth = 18;
        for (int i = 0; i < getChildCount(); i++) {
            View view = getChildAt(i);
            int viewWidth = view.getWidth();
            int viewHeight = view.getHeight();
            if (disWidth + viewWidth > width) {
                row++;
                disWidth = 18;
            }
            view.layout(disWidth, row * viewHeight, viewWidth + disWidth, viewHeight * (row + 1));
            disWidth += viewWidth;
        }
    }

Activity

helper = new MyHelper(MainActivity.this);
        database = helper.getReadableDatabase();
        mText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                String s = mEdit01.getText().toString();
                database.execSQL("insert into user(name)values(?)", new String[]{s});
                list.add(s);
                TextView textView = new TextView(MainActivity.this);
                textView.setText(list.get(list.size() - 1));
                textView.setTextSize(30);
                textView.setPadding(25, 15, 25, 15);
                mMainFlow.addView(textView, params);
            }
        });

猜你喜欢

转载自blog.csdn.net/LG_lxb/article/details/84654130