流程布局问题

时间:2015-01-08 10:29:11

标签: android flowlayout

我正在使用此流布局类在多行中排列动态创建的TextView,但我遇到了以下问题:

  1. 此布局的高度不会表现为WRAP_CONTENT(如图所示) 链接中给出的图像)。
  2. 我将此布局toRightOf放置为另一个静态 TextViews(,抄送)。它在左边留下一个等于TextView宽度的边距 在每一行(如链接中给出的图像所示)。
  3. 我希望在内容切换到下一行时删除此边距。如何解决这个问题?

    任何帮助都将不胜感激。

    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;
                }
            }
        }
    

    我在我的xml中使用它:

    <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>
    

0 个答案:

没有答案