我在绝对布局上动态添加文本视图。以下是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 :(得分:1)
您无法使用getWidth
和getHeight
,在测量孩子后应使用getMeasuredWidth
和getMeasuredHeight
答案 1 :(得分:0)
你可以在布局(.xml)
中调整它android:layout_width="wrap_content"
android:layout_height="wrap_content"
你可以像'&#34; 100dp&#34;等