Android流式布局(自定义View)

public class LiuLayout extends ViewGroup {
    int maxHeight;
    int marginLeft = 20;
    int marginTop = 20;
    private View childAt;
    private int measuredWidth;

    public LiuLayout(Context context) {
        super(context);
    }

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

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        findMaxHeight();
        int left = 0;
        int top = 0;
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View childAt = getChildAt(i);
            int measuredWidth = childAt.getMeasuredWidth();
            if (left != 0) {
                if ((left + measuredWidth) > widthSize) {
                    top = marginTop + maxHeight;
                    left = 0;
                }
            }
            left = marginLeft + measuredWidth + left;
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        findMaxHeight();
        int left = 0;
        int top = 0;
        int childCount = getChildCount();
        for (int j = 0; j < childCount; j++) {
            childAt = getChildAt(j);
            measuredWidth = childAt.getMeasuredWidth();
            int width = getWidth();
            if (left != 0) {
                if ((left + measuredWidth) > width) {
                    top = marginTop + maxHeight + top;
                    left = 0;
                }
            }
            childAt.layout(left, top, left + measuredWidth, top + maxHeight);
            left = left + measuredWidth + marginLeft;
        }

    }

    private void findMaxHeight() {
        maxHeight = 0;
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View childAt = getChildAt(i);
            int measuredHeight = childAt.getMeasuredHeight();
            if (maxHeight < measuredHeight) {
                maxHeight = measuredHeight;
            }
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".view.activity.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/editText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <Button
            android:id="@+id/buttons"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="搜索" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="跳转" />
    </LinearLayout>

    <com.bwei.zhaolingxiao20190308.view.zidingyi.LiuLayout
        android:id="@+id/liuLayout"
        android:layout_width="match_parent"
        android:layout_height="400dp"></com.bwei.zhaolingxiao20190308.view.zidingyi.LiuLayout>

    <Button
        android:id="@+id/buttoncle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="清空" />

</LinearLayout>
public class MainActivity extends AppCompatActivity {

    private LiuLayout liuLayout;
    private Button buttonclear;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buttonclear = findViewById(R.id.buttoncle);
        //流式布局
        final EditText editText = findViewById(R.id.editText);
        Button buttons = findViewById(R.id.buttons);
        liuLayout = findViewById(R.id.liuLayout);
        buttons.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String strings = editText.getText().toString();
                TextView textView = new TextView(MainActivity.this);
                textView.setText(strings);
                liuLayout.addView(textView);
            }
        });
        //清空流式布局
        buttonclear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                liuLayout.removeAllViews();
            }
        });
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43731179/article/details/87924240