TextView.setGravity(Gravity.CENTER)在自定义ViewGroup中不起作用

时间:2018-09-29 11:36:51

标签: android textview android-gravity

我创建了一个自定义ViewGroup来实现以下UI。 但是我不知何故得到了文本视图未居中的textview。 我尝试了很多方法,但不幸的是失败了。

enter image description here enter image description here enter image description here 这是我的自定义视图组

/**
 * Used for showing second sift
 */
public class SecondSiftContainerView extends ViewGroup {
    @Nullable
    private List<MultiOptInfo.SecondLevelSiftItem> mSecondLevelSiftItemList;

    private List<Integer> mLineNumList = new ArrayList<>();


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (getChildCount() >= 4) {
        } else {
            int mPaddingTop = getPaddingTop();
            int mPaddingBottom = getPaddingBottom();

            int widthSize = MeasureSpec.getSize(widthMeasureSpec);
            int heightMode = MeasureSpec.getMode(heightMeasureSpec);
            int heightSize = MeasureSpec.getSize(heightMeasureSpec);
            int lineHeight = 0;
            for (int i = 0; i < this.getChildCount(); i++) {
                View child = this.getChildAt(i);
                if (child.getVisibility() == GONE) {
                    continue;
                }
                measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, mPaddingTop);
                MarginLayoutParams marginLayoutParams =
                        (MarginLayoutParams) child.getLayoutParams();
                int childHeight = child.getMeasuredHeight();
                int spaceHeight =
                        marginLayoutParams.topMargin + childHeight
                                + marginLayoutParams.bottomMargin;
                if (spaceHeight > lineHeight) {
                    lineHeight = spaceHeight;
                }
            }
            setMeasuredDimension(widthSize,
                    heightMode == MeasureSpec.EXACTLY ? heightSize :
                            mPaddingTop + lineHeight + mPaddingBottom
            );
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int mPaddingLeft = getPaddingLeft();
        int mPaddingRight = getPaddingRight();
        int mPaddingTop = getPaddingTop();

        int lineX = mPaddingLeft;
        int lineWidth = r - l;
        int lineUsed = mPaddingLeft + mPaddingRight;
        int lineHeight = 0;
        int lineNum = 0;

        mLineNumList.clear();
        if (getChildCount() == 2) {
            View child0 = getChildAt(0);
            int child0width = (getMeasuredWidth() - 30 * 3) / 2;
            child0.layout(30, 30, child0width + 30, 30 + child0.getMeasuredHeight());

            View child1 = getChildAt(1);
            child1.layout(child0width + 30 + 40, 30, 30 + 40 + child0width * 2,
                    30 + child1.getMeasuredHeight());
        } else if (getChildCount() == 3) {
            int viewWidth = (getMeasuredWidth() - 30 * 4) / 3;
            View child0 = getChildAt(0);
            child0.layout(30, 30, 30 + viewWidth, 30 + child0.getMeasuredHeight());

            View child1 = getChildAt(1);
            child1.layout(30 * 2 + viewWidth, 30, 30 * 2 + viewWidth * 2,
                    30 + child1.getMeasuredHeight());

            View child2 = getChildAt(2);
            child2.layout(30 * 3 + viewWidth * 2, 30, 30 * 3 + viewWidth * 3,
                    30 + child2.getMeasuredHeight());
        } else if (getChildCount() >= 4) {
            for (int i = 0; i < getChildCount(); i++) {
                View child = this.getChildAt(i);
                if (child.getVisibility() == GONE) {
                    continue;
                }
                int spaceWidth = 0;
                int spaceHeight = 0;
                int left;
                int top;
                int right;
                int bottom;
                int childWidth = child.getMeasuredWidth();
                int childHeight = child.getMeasuredHeight();

                LayoutParams childLayoutParams = child.getLayoutParams();
                if (childLayoutParams instanceof MarginLayoutParams) {
                    MarginLayoutParams marginLayoutParams = (MarginLayoutParams) childLayoutParams;
                    spaceWidth = marginLayoutParams.leftMargin + marginLayoutParams.rightMargin;
                    spaceHeight = marginLayoutParams.topMargin + marginLayoutParams.bottomMargin;
                    left = lineX + marginLayoutParams.leftMargin;
                    top = mPaddingTop + marginLayoutParams.topMargin;
                    right = lineX + marginLayoutParams.leftMargin + childWidth;
                    bottom = mPaddingTop + marginLayoutParams.topMargin + childHeight;
                } else {
                    left = lineX;
                    top = mPaddingTop;
                    right = lineX + childWidth;
                    bottom = mPaddingTop + childHeight;
                }
                spaceWidth += childWidth;
                spaceHeight += childHeight;

                if (lineUsed + spaceWidth > lineWidth) {
                    break;
                }
                child.layout(left, top, right, bottom);
                lineNum++;
                if (spaceHeight > lineHeight) {
                    lineHeight = spaceHeight;
                }
                lineUsed += spaceWidth;
                lineX += spaceWidth;
            }
            // add the num of last line
            mLineNumList.add(lineNum);
        }
    }

    public void setSecondLevelSiftItemList(@NonNull
            List<MultiOptInfo.SecondLevelSiftItem> secondLevelSiftItemList) {
        mSecondLevelSiftItemList = secondLevelSiftItemList;
        int itemListSize = mSecondLevelSiftItemList.size();
        if (itemListSize == 2 || itemListSize == 3) {
            initTwoOrThreeTextView();
        } else if (itemListSize >= 4) {
            initFourOrAboveTextView();
        }
    }

    private void initTwoOrThreeTextView() {
        List<TextView> tagTextViewList = new ArrayList<>();
        assert mSecondLevelSiftItemList != null;
        for (int i = 0; i < mSecondLevelSiftItemList.size(); i++) {
            TextView textView = new TextView(getContext());
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
            textView.setTextColor(Styles.parseColor("#333333"));
            textView.setBackgroundColor(getResources().getColor(R.color.white));
            LinearLayout.LayoutParams layoutParams =
                    new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.gravity = Gravity.CENTER;
            textView.setLayoutParams(layoutParams);
            textView.setText(mSecondLevelSiftItemList.get(i).itemValue);
            layoutParams.setMargins(0, 30, 0, 30);
            int verticalPadding = DensityUtil.dip2px(textView.getContext(), 4);
            int horizontalPadding = DensityUtil.dip2px(textView.getContext(), 0);
            textView.setPadding(horizontalPadding, verticalPadding, horizontalPadding,
                    verticalPadding);
            textView.setGravity(Gravity.CENTER);

            tagTextViewList.add(textView);
            final int finalI = i;
            textView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    ToastUtils.showToast(getContext(),
                            mSecondLevelSiftItemList.get(finalI).itemValue);
                }
            });
        }

        for (int i = 0; i < tagTextViewList.size(); i++) {
            addView(tagTextViewList.get(i));
        }
    }
}

这是真实的显示内容:

enter image description here

0 个答案:

没有答案