自定义view动态加载控件实现动态换行

如下效果:



实现:

1,xml文件

<LinearLayout
        android:id="@+id/ll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"/>

2,自定义的ViewGroup

public class MyViewGroup extends ViewGroup {

    private final static String TAG = "MyViewGroup";

    private final static int VIEW_MARGIN = 2;

    public MyViewGroup(Context context) {

        super(context);

    }

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

        Log.d(TAG, "widthMeasureSpec = " + widthMeasureSpec
                + " heightMeasureSpec" + heightMeasureSpec);

        for (int index = 0; index < getChildCount(); index++) {

            final View child = getChildAt(index);

            // measure

            child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);

        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    @Override
    protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {

        Log.d(TAG, "changed = " + arg0 + " left = " + arg1 + " top = " + arg2
                + " right = " + arg3 + " botom = " + arg4);

        final int count = getChildCount();

        int row = 0;// which row lay you view relative to parent

        int lengthX = arg1; // right position of child relative to parent

        int lengthY = arg2; // bottom position of child relative to parent

        for (int i = 0; i < count; i++) {

            final View child = this.getChildAt(i);

            int width = child.getMeasuredWidth();

            int height = child.getMeasuredHeight();

            lengthX += width + VIEW_MARGIN;

            lengthY = row * (height + VIEW_MARGIN) + VIEW_MARGIN + height
                    + arg2;

            // if it can't drawing on a same line , skip to next line

            if (lengthX > arg3) {

                lengthX = width + VIEW_MARGIN + arg1;

                row++;

                lengthY = row * (height + VIEW_MARGIN) + VIEW_MARGIN + height
                        + arg2;

            }

            child.layout(lengthX - width, lengthY - height, lengthX, lengthY);

        }

    }

}

3, MainActivity中添加

public class MainActivity extends AppCompatActivity {
    private LinearLayout ll;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        MyViewGroup myViewGroup = new MyViewGroup(this);

        String[] str = new String[]{"aaaaa", "aaa", "aaaafgga", "aaaaafsfsfasfaasf", "aaaaa", "aaaaa", "aaaaqweqweqweqweqwea", "aaaaa", "qwewqeqweqwe", "aaaaa"};
        for (int i = 0; i < str.length; i++) {
            Button button = new Button(this);
            button.setText(str[i]);
            myViewGroup.addView(button);
        }
        ll = findViewById(R.id.ll);
        ll.addView(myViewGroup);

    }
}


猜你喜欢

转载自blog.csdn.net/u011368551/article/details/79859713