Layout Issues

I use this stream layout class to order dynamically generated TextViewin multiple lines, but I have the following problems:

  • The height of this layout does not behave as WRAP_CONTENT(as shown in the image indicated in the link).
  • I post this layout toRightOfanother static TextViews(To, Cc). It leaves the box to the left equal to the width TextView in each row (as shown in the image referenced in the link).

I want to remove this margin when the content switches to the next line. How to solve this problem?

Any help would be appreciated.

enter image description here

 public class FlowLayout extends ViewGroup 
    {         
        private int paddingHorizontal;   

        public FlowLayout(Context context) 
        {
            super(context);
            init();
        }

        public FlowLayout(Context context, AttributeSet attrs) 
        {
            this(context, attrs, 0);
        }

        public FlowLayout(Context context, AttributeSet attrs, int defStyle) 
        {
            super(context, attrs, defStyle);
            init();
        }

        private void init()
        {
            paddingHorizontal = getResources().getDimensionPixelSize(R.dimen.flowlayout_horizontal_padding);
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
        {
            int childLeft = getPaddingLeft();
            int childTop = getPaddingTop();
            int lineHeight = 0;
            int myWidth = resolveSize(100, widthMeasureSpec);
            int wantedHeight = 0;
            for (int i = 0; i < getChildCount(); i++) 
            {
                final View child = getChildAt(i);
                if (child.getVisibility() == View.GONE) 
                {
                    continue;
                }
                child.measure(getChildMeasureSpec(widthMeasureSpec, 0, child.getLayoutParams().width), getChildMeasureSpec(heightMeasureSpec, 0, child.getLayoutParams().height));
                int childWidth = child.getMeasuredWidth();
                int childHeight = child.getMeasuredHeight();
                lineHeight = Math.max(childHeight, lineHeight);
                if (childWidth + childLeft + getPaddingRight() > myWidth) 
                {
                    childLeft = getPaddingLeft();
                    childTop += lineHeight;
                    lineHeight = childHeight;
                }
                childLeft += childWidth + paddingHorizontal;
            }
            wantedHeight += childTop + lineHeight + getPaddingBottom();
            setMeasuredDimension(myWidth, resolveSize(wantedHeight, heightMeasureSpec));
        }

        @Override
        protected void onLayout(boolean changed, int left, int top, int right, int bottom) 
        {
            int childLeft = getPaddingLeft();
            int childTop = getPaddingTop();
            int lineHeight = 0;
            int myWidth = right - left;
            for (int i = 0; i < getChildCount(); i++) 
            {
                final View child = getChildAt(i);
                if (child.getVisibility() == View.GONE) 
                {
                    continue;
                }
                int childWidth = child.getMeasuredWidth();
                int childHeight = child.getMeasuredHeight();
                lineHeight = Math.max(childHeight, lineHeight);
                if (childWidth + childLeft + getPaddingRight() > myWidth) 
                {
                    childLeft = getPaddingLeft();
                    childTop += lineHeight;
                    lineHeight = childHeight;
                }
                child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
                childLeft += childWidth + paddingHorizontal;
            }
        }
    }

I use it in my xml as follows:

<RelativeLayout
    android:id="@+id/main_relative_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <RelativeLayout
        android:id="@+id/To_textView_wrapper_relative_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/To_textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:text="@string/to"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="5dp" />

        <PackageName.FlowLayout
            android:id="@+id/flow_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/To_textView" />

    </RelativeLayout> 

    <RelativeLayout
        android:id="@+id/Cc_textView_wrapper_relative_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/To_textView_wrapper_relative_layout" >

        <TextView
            android:id="@+id/PhoneNo_textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:text="@string/cc"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="5dp" />

        <PackageName.FlowLayout
            android:id="@+id/flow_container1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/Cc_textView" />

    </RelativeLayout> 

    <RelativeLayout
        android:id="@+id/Bcc_textView_wrapper_relative_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/PhoneNo_textView_wrapper_relative_layout" >

        <TextView
            android:id="@+id/Bcc_textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:text="@string/bcc"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="5dp" />

        <PackageName.FlowLayout
            android:id="@+id/flow_container2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/Bcc_textView" />

    </RelativeLayout> 

</RelativeLayout>
+4
source share

Source: https://habr.com/ru/post/1570445/


All Articles