【发布时间】:2015-03-26 11:45:06
【问题描述】:
我有一个自定义的Linearlayout,它将一些视图组合在一起。我希望这个Linearlayout 是wrap_content 的高度。我尝试像这样在 custrucor 中添加布局参数
LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
setLayoutParams(params);
但它没有效果。高度仍然是match_parent。我还尝试根据这样的孩子的身高计算onMeasure中的身高
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
measureChildren(widthMeasureSpec,heightMeasureSpec);
int size = 0;
for(int i = 0; i <getChildCount();i++) {
size += getChildAt(i).getMeasuredHeight();
}
int height = resolveSize(size,heightMeasureSpec);
setMeasuredDimension(widthMeasureSpec,height);
}
它也没有任何作用。那么问题出在哪里?
【问题讨论】:
-
您是否正在为任何孩子使用重量?你为这个 LinearLayout 子类使用什么方向?
-
我使用的是垂直方向,没有重量。计算儿童身高是正确的,但不适用
标签: java android view android-custom-view onmeasure