我正在尝试创建类似于主屏幕布局的东西。这由单个水平方向的 LinearLayout 中的多个垂直方向的 LinearLayout 组成,所有这些都位于 HorizontalScrollView 中。我将其编写为一个名为“HomeLayout”的自定义类,它扩展了 HorizontalScrollView。
LinearLayout wrapper = new LinearLayout(getContext());
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
wrapper.setLayoutParams(params);
wrapper.setOrientation(LinearLayout.HORIZONTAL);
addView(wrapper);
for(int i = 0; i < 2; i++) {
LinearLayout linear = (LinearLayout) View.inflate(getContext(), R.layout.screens, null);
linear.setLayoutParams(params);
wrapper.addView(linear);
}
问题是添加时“screen2”的宽度为“0”。
这只适用于我在 onDraw() 中调用它并使用 getMeasuredWidth() 和 getMeasuredHeight() 而不是 LayoutParams.FILL_PARENT。即使那样,我也不确定这是否是正确的做法。
此外,我无法在 onCreate() 中引用视图 'wrapper'、'screen1'、'screen2'。
我松散地遵循了这个链接:http ://blog.velir.com/index.php/2010/11/17/android-snapping-horizontal-scroll/
我究竟做错了什么?