Cómo restablecer solo una subvista en onMeasure para que una fila llene la pantalla, si hay varias filas adaptables

Cómo restablecer solo una subvista en onMeasure para que una fila llene la pantalla, si hay varias filas adaptables

Puede intentar anular el método o en onMeasureel método para cumplir con este requisito.measureChildWithMarginsmeasureChild

Para una Vista con una sola palabra, podemos establecer su ancho al ancho de la pantalla y su alto a la altura máxima, de modo que la Vista llene una fila completa.

Para situaciones adaptables de múltiples Vistas, primero podemos medir el ancho de cada Vista y sumarlo para obtener su ancho total. Si el ancho total es menor que el ancho de la pantalla, entonces podemos establecer el ancho de la Vista en el ancho de la pantalla dividido por el número de Vistas, de modo que el ancho de todas las Vistas sea el mismo y se pueda colocar en una línea. De lo contrario, podemos establecer de forma predeterminada el ancho original y dejar que se divida en varias líneas.

Aquí hay un código de muestra:

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

    // 获取父容器的尺寸规格
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    int childCount = getChildCount();
    if (childCount == 1) {  // 只有一个 View 的情况
        View child = getChildAt(0);

        // 让 View 的宽度填满屏幕,高度取最大值
        int widthMeasureSpecForChild = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);
        int heightMeasureSpecForChild = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        child.measure(widthMeasureSpecForChild, heightMeasureSpecForChild);

        setMeasuredDimension(widthSize, child.getMeasuredHeight());
    } else {  // 多个 View 的情况
        int totalWidth = 0;

        // 计算多个 View 的总宽度
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);

            int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
            int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
            child.measure(childWidthMeasureSpec, childHeightMeasureSpec);

            int childWidth = child.getMeasuredWidth();
            totalWidth += childWidth;
        }

        // 如果总宽度小于屏幕宽度,让每一个 View 的宽度都为屏幕宽度除以 View 的个数
        if (totalWidth < widthSize) {
            for (int i = 0; i < childCount; i++) {
                View child = getChildAt(i);

                int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(widthSize / childCount, MeasureSpec.EXACTLY);
                int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
                child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
            }
        }

        // 测量自身尺寸并设置
        int totalHeight = 0;
        int lineWidth = 0;
        int lineHeight = 0;
        int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);

            // 测量子 View 的尺寸
            child.measure(childWidthMeasureSpec, childHeightMeasureSpec);

            // 计算子 View 的行宽和行高
            int childWidth = child.getMeasuredWidth();
            int childHeight = child.getMeasuredHeight();
            if (lineWidth + childWidth > widthSize) {
                totalHeight += lineHeight;
                lineWidth = 0;
                lineHeight = childHeight;
            }
            lineWidth += childWidth;
            lineHeight = Ma

Supongo que te gusta

Origin blog.csdn.net/ck3345143/article/details/130429121
Recomendado
Clasificación