2014-07-21 71 views
1

我在絕對佈局上動態添加文本視圖。以下是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)); 
} 
+0

只是檢查此問題http://stackoverflow.com/questions/6940765/why-setting-text-from-onmeasure-does-not-affe ct-textview – Sree

回答

0

你可以調整這個在佈局(.XML)

android:layout_width="wrap_content" 
android:layout_height="wrap_content" 

你可以把它像 「100dp」 等

+0

我正在以編程方式添加... – Asthme

1

不能使用getWidthgetHeight,你應該使用getMeasuredWidthgetMeasuredHeight測量孩子後

+0

它是getMeasuredWidth和Height.it無法正常工作。所以這不是問題 – Asthme