确定视图是否部分在屏幕外

v4r*_*v4r 5 android

我有一个动态添加的视图.有时,视图仅部分可见,因为其底部不在屏幕上.在这种情况下,我想要移动视图.但是,我不知道如何检测它是否是屏幕外的以及是多少.

编辑:这个问题的上下文是我有一个edittext,我想在它旁边显示一个自定义软键盘.这是我用来移动自定义键盘的代码.

 public void moveKeyboardNextToView(View view) {
    int[] location = new int[]{0, 0};
    view.getLocationInWindow(location);
    Rect r = new Rect();
    view.getGlobalVisibleRect(r);

    int height = r.bottom - r.top;
    int newTop = r.bottom - view.getHeight() / 2 - this.mKeyboardView.getHeight() / 2;
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) this.mKeyboardView.getLayoutParams();
    params.setMargins(0, newTop, 0, 0);
    this.mKeyboardView.setLayoutParams(params);
    this.mKeyboardView.invalidate();

    this.mKeyboardView.post(new Runnable() {
        @Override
        public void run() {
            int[] location = new int[2];
            Rect r2 = new Rect();
            mKeyboardView.getLocalVisibleRect(r2);
            double abc = r2.bottom;
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

nds*_*ter 2

我这样做的方法是使用以下信息进行计算:

如果您掌握了这些信息,那么计算您需要的一切就不再那么困难了。只需确保每个值使用相同的单位(像素、dpi 等)即可。

  • 对我不起作用。getMeasuredHeight() 给出裁剪视图的高度。 (5认同)