如何从RelativeLayout中的视图获取绝对坐标 [英] How to get absolute coordinates from Views in a RelativeLayout

查看:112
本文介绍了如何从RelativeLayout中的视图获取绝对坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下布局:

<ScrollView>
    <RelativeLayout>
        <RelativeLayout>
            <View
                android:id="@+id/vline"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/md_white_1000"/>
            <ImageButton>...</ImageButton>
        </RelativeLayout>
    </RelativeLayout>
</ScrollView>

当然,不同的布局在不同的屏幕上都有自己的内容和高度.

The different Layouts have there own content and height on different screens of course.

现在,我想通过按ImageButton来向上平滑滚动ScrollView,以使Views的左上角位于特定屏幕的左上角.

Now I want, by pressing the ImageButton, to smoothly scroll my ScrollView up so that the Views upper left corner is in the upper left corner of the specific screen.

我尝试过:

ImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scrollView.smoothScrollTo(0, vline.getTop());
            }
        });

和所有其他"getY()"方法,但是没有人带来我想要的结果.它只是将我所有的内容全部滚动到高水平.

and all the other "getY()" methods, but no one bring the result I want to have. It just scrolls all of my content fully high.

我刚刚开始使用Android,谢谢您的帮助.

I'm just getting started with Android so thank you for your help.

推荐答案

这是解决方案:

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int statusbarHeight = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getHeight()
                        - YOURVIEW.getHeight();
                int[] position = new int[2];
                vline.getLocationInWindow(position);
                scrollView.smoothScrollBy(0, (position[1]-statusbarHeight));
            }
        });

这篇关于如何从RelativeLayout中的视图获取绝对坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆