Android:向 HorizontalScrollView 添加布局
我正在尝试创建类似于主屏幕布局的内容。它由单个水平方向的 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/
我做错了什么?
I'm trying to create something similar to the home screen layout. This consists of multiple vertically oriented LinearLayouts within a single horizontally oriented LinearLayout, all housed in a HorizontalScrollView. I've written it as a custom class called 'HomeLayout' extending 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);
}
The problem is 'screen2' has width '0' when added.
This only works if I call this in onDraw() and use getMeasuredWidth() and getMeasuredHeight() instead of LayoutParams.FILL_PARENT. And even then, I'm not sure if that's the right thing to do.
Furthermore I'm unable to reference the views 'wrapper', 'screen1', 'screen2' in onCreate().
I loosely followed this link: http://blog.velir.com/index.php/2010/11/17/android-snapping-horizontal-scroll/
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
希望这对其他人有帮助。我通过以下方式实现 onMeasure() 解决了我的问题:
Hoping this helps others. I solved my problem by implementing onMeasure() in the following way: