在relativeLayout中依次添加几个textViews(可变宽度)

时间:2016-07-21 09:35:24

标签: android android-layout listview android-relativelayout

我有一个相对布局部分。在其中,我必须以编程方式添加几个可以在宽度上变化的可点击文本视图。我需要他们一个接一个地出现。如果textView的宽度不能适合当前行的剩余空间,则它应该以类似的方式开始在行下方填充。

我需要这样的事情:illustration of what i want

到目前为止我所管理的是以下代码:

public static void updateWordsListingOnUi(final ArrayList<String> wordsList) { //TODO: incomplete and may be inefficient
    mUiHandler.post(new Runnable() {
        @Override
        public void run() {
            TextView textView;

            wordsListingContainer.removeAllViewsInLayout(); //TODO
            for (final String word : wordsList)
            {
                textView = new TextView(context);
                textView.setText(word);
                textView.setClickable(true);
                textView.setPadding(1,1,10,1);
                if (wordsList.indexOf(word)%2 == 0) {
                    textView.setBackgroundColor(context.getResources().getColor(R.color.colorPrimary));
                }
                else {
                    textView.setBackgroundColor(context.getResources().getColor(R.color.colorAccent));
                }
                textView.setId(wordsList.indexOf(word)); //give IDs from 0 - (maxSize-1)

                textView.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Toast.makeText(context, word, Toast.LENGTH_SHORT).show();
                    }
                });
                if(wordsList.indexOf(word) > 0) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                    layoutParams.addRule(RelativeLayout.RIGHT_OF, textView.getId()-1); //TODO
                    wordsListingContainer.addView(textView,layoutParams);
                }
                else {
                    wordsListingContainer.addView(textView);
                }

            }
        }
    });
}

这部分地完成了这项工作。它将文本视图一个接一个地放置,但是当我没有足够的空间时,它不会换行到下一行。我想不出我怎么能做到这一点。

2 个答案:

答案 0 :(得分:3)

Here is a tutorial, that explains how, you can do this

简而言之,你的课程必须是:

public class TagLayout extends ViewGroup {
    int deviceWidth;

    public TagLayout(Context context) {
        this(context, null, 0);
    }

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

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

    private void init(Context context) {
        final Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        Point deviceDisplay = new Point();
        display.getSize(deviceDisplay);
        deviceWidth = deviceDisplay.x;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int count = getChildCount();
        int curWidth, curHeight, curLeft, curTop, maxHeight;

        //get the available size of child view
        final int childLeft = this.getPaddingLeft();
        final int childTop = this.getPaddingTop();
        final int childRight = this.getMeasuredWidth() - this.getPaddingRight();
        final int childBottom = this.getMeasuredHeight() - this.getPaddingBottom();
        final int childWidth = childRight - childLeft;
        final int childHeight = childBottom - childTop;

        maxHeight = 0;
        curLeft = childLeft;
        curTop = childTop;

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

            if (child.getVisibility() == GONE)
                return;

            //Get the maximum size of the child
            child.measure(MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.AT_MOST));
            curWidth = child.getMeasuredWidth();
            curHeight = child.getMeasuredHeight();
            //wrap is reach to the end
            if (curLeft + curWidth >= childRight) {
                curLeft = childLeft;
                curTop += maxHeight;
                maxHeight = 0;
            }
            //do the layout
            child.layout(curLeft, curTop, curLeft + curWidth, curTop + curHeight);
            //store the max height
            if (maxHeight < curHeight)
                maxHeight = curHeight;
            curLeft += curWidth;
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int count = getChildCount();
        // Measurement will ultimately be computing these values.
        int maxHeight = 0;
        int maxWidth = 0;
        int childState = 0;
        int mLeftWidth = 0;
        int rowCount = 0;

        // Iterate through all children, measuring them and computing our dimensions
        // from their size.
        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);

            if (child.getVisibility() == GONE)
                continue;

            // Measure the child.
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
            maxWidth += Math.max(maxWidth, child.getMeasuredWidth());
            mLeftWidth += child.getMeasuredWidth();

            if ((mLeftWidth / deviceWidth) > rowCount) {
                maxHeight += child.getMeasuredHeight();
                rowCount++;
            } else {
                maxHeight = Math.max(maxHeight, child.getMeasuredHeight());
            }
            childState = combineMeasuredStates(childState, child.getMeasuredState());
        }

        // Check against our minimum height and width
        maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
        maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());

        // Report our final dimensions.
        setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState),
                resolveSizeAndState(maxHeight, heightMeasureSpec, childState << MEASURED_HEIGHT_STATE_SHIFT));
    }
}

答案 1 :(得分:2)

为什么不尝试使用库,我在这里分享一些链接

Chips EditText Library
MultiTextTagView
TokenAutoComplete
Android Chips

这些库将帮助您实现输出。