【发布时间】:2012-10-04 02:24:07
【问题描述】:
我有一个 HorizontalScrollView,其中包含一个包含一些自定义视图的 LinearLayout。现在,我通过这一行(在孩子的构造函数中)固定孩子的大小:
setLayoutParams(new LayoutParams(200, LayoutParams.FILL_PARENT));
我想做的是将组件的宽度设置为自身高度的 30%,而不是实际的 200px。但我仍然需要“FILL_PARENT”的高度。
当我覆盖 onMeasure 方法时,我尝试了:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
System.out.println(widthMeasureSpec + "/" + heightMeasureSpec);
}
但我得到了输出:
1073742024/1073742586
所以看起来我在这里做不了很多事情,但我仍然没有很多信息:)
知道怎么做吗?
我确切地说我正在模拟器上进行测试(不知道这是否重要)。
【问题讨论】:
-
这应该足够了:
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int height = getMeasuredHeight(); super.onMeasure(MeasureSpec.makeMeasureSpec(height / 3, MeasureSpec.EXACTLY), heightMeasureSpec); }.
标签: android width children horizontalscrollview