如何在Android自定义视图中实现滚动

发布于 2024-11-25 08:17:45 字数 880 浏览 1 评论 0原文

我编写了一个自定义视图,我想确保它可以在任何屏幕尺寸上查看。

我已经重写了 onMeasure:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
    int parentHeight = MeasureSpec.getSize(heightMeasureSpec);

    this.setMeasuredDimension(parentWidth, parentHeight);
}

当视图小于屏幕时,这似乎工作正常。但有时,自定义视图比屏幕大,而我计划使用较小的屏幕尺寸,因此我将自定义视图包装在 ScrollView 中,但现在使用 parentHeight onMeasure 中的结果为 0。

我已将自定义视图的超类从 View 更改为 ScrollView,希望能够轻松获胜继承了滚动功能,但这还没有发生所以我只能尝试找到一种方法让 ScrollView 功能与我的自定义视图配合使用,或者编写我自己的滚动功能。

有人有什么建议吗?我看过这篇关于使用低级触摸事件进行大图像滚动的文章并且打算如果我被迫编写自己的功能,请复制其中的一些功能,但无论怎样,我都会感激朝正确方向的推动。

I've written a custom view which I'd like to ensure is viewable on any screen size.

I've overridden the onMeasure:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
    int parentHeight = MeasureSpec.getSize(heightMeasureSpec);

    this.setMeasuredDimension(parentWidth, parentHeight);
}

and this seems to work fine when the view is smaller than the screen. Sometimes, though, the custom view is larger than the screen, and I'm planning for smaller screen sizes, so I wrapped the custom view in a ScrollView but now the parentHeight in the onMeasure comes out as 0.

I've changed the superclass of the custom view from View to ScrollView, hoping for an easy win of inheriting the scrolling functionality, but this hasn't happened so I'm left with trying to find a way of getting the ScrollView functionality to work with my custom view, or writing my own scrolling functionality.

Has anyone any advice? I've seen this post on Large Image Scrolling Using Low Level Touch Events and was going to copy some of that functionality if I'm forced to write my own, but would appreciate a nudge in the right direction either way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

情场扛把子 2024-12-02 08:17:45

事实证明答案很简单。我保留了 ScrollView 并更改了 onMeasure。需要注意的是,虽然 Android 会提供宽度,但它不会为我提供高度,这最初是令人困惑的。为了让视图填充可用空间,我抓住了父视图的可见矩形。完整代码(希望它能帮助其他遇到同样问题的人):

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
    int parentHeight;

    if(isLandscape()) {
        Rect r = new Rect();
        ((ScrollView)getParent()).getGlobalVisibleRect(r);
        parentHeight = r.bottom - r.top;
    } else {
        parentHeight = (int) Util.getViewHeight();
    }

    this.setMeasuredDimension(parentWidth, parentHeight);
}

Turns out the answer was simple. I left the ScrollView in and changed my onMeasure. The thing to note about it is that although Android would supply the width, it wouldn't supply me with a height, which was initially confusing. To just get the view to fill the available space I grabbed the visible rect of the parent view. Code in full (hopefully it'll help someone else who is having the same problem):

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
    int parentHeight;

    if(isLandscape()) {
        Rect r = new Rect();
        ((ScrollView)getParent()).getGlobalVisibleRect(r);
        parentHeight = r.bottom - r.top;
    } else {
        parentHeight = (int) Util.getViewHeight();
    }

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