Android:向 Horizo​​ntalScrollView 添加布局

发布于 2024-11-20 00:29:32 字数 1174 浏览 4 评论 0原文

我正在尝试创建类似于主屏幕布局的内容。它由单个水平方向的 LinearLayout 内的多个垂直方向的 LinearLayout 组成,所有这些都位于 Horizo​​ntalScrollView 中。我将其编写为一个名为“HomeLayout”的自定义类,扩展了 Horizo​​ntalScrollView。

hierarchyviewer

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-horizo​​ntal-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.

hierarchyviewer

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少年亿悲伤 2024-11-27 00:29:32

希望这对其他人有帮助。我通过以下方式实现 onMeasure() 解决了我的问题:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);
    setMeasuredDimension(width, height);

    for(int i = 0; i < mWrapper.getChildCount(); i++) {
        View child = mWrapper.getChildAt(i);
        child.setLayoutParams(new LinearLayout.LayoutParams(width, 
        LayoutParams.FILL_PARENT));
    }

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

Hoping this helps others. I solved my problem by implementing onMeasure() in the following way:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);
    setMeasuredDimension(width, height);

    for(int i = 0; i < mWrapper.getChildCount(); i++) {
        View child = mWrapper.getChildAt(i);
        child.setLayoutParams(new LinearLayout.LayoutParams(width, 
        LayoutParams.FILL_PARENT));
    }

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文