我在絕對佈局上動態添加文本視圖。以下是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));
}
只是檢查此問題http://stackoverflow.com/questions/6940765/why-setting-text-from-onmeasure-does-not-affe ct-textview – Sree