文字视图的高度和宽度没有变化?

时间:2014-07-21 10:22:32

标签: android typeface

我在绝对布局上动态添加文本视图。以下是absoulute布局的代码。它的工作。但问题是当我增加文本视图文本的大小时。然后文本视图的宽度和高度不会改变。有人请帮忙。

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int count = getChildCount();

    int maxHeight = 0;
    int maxWidth = 0;

    // Find out how big everyone wants to be
    measureChildren(widthMeasureSpec, heightMeasureSpec);

    // Find rightmost and bottom-most child
    for (int i = 0; i < count; i++) {
        View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            int childRight;
            int childBottom;

            LayoutParams lp
                    = (LayoutParams) child.getLayoutParams();

            childRight = lp.x + child.getWidth();
            childBottom = lp.y + child.getHeight();

            maxWidth = Math.max(maxWidth, childRight);
            maxHeight = Math.max(maxHeight, childBottom);
        }
    }

    // Account for padding too
    maxWidth += getPaddingLeft () + getPaddingRight ();
    maxHeight += getPaddingTop () + getPaddingBottom ();
    /* original
    maxWidth += mPaddingLeft + mPaddingRight;
    maxHeight += mPaddingTop + mPaddingBottom;
    */

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

    setMeasuredDimension(resolveSize(maxWidth, widthMeasureSpec),
            resolveSize(maxHeight, heightMeasureSpec));
}

2 个答案:

答案 0 :(得分:1)

您无法使用getWidthgetHeight,在测量孩子后应使用getMeasuredWidthgetMeasuredHeight

答案 1 :(得分:0)

你可以在布局(.xml)

中调整它
android:layout_width="wrap_content"
 android:layout_height="wrap_content"

你可以像'&#34; 100dp&#34;等