美文网首页
getLocationInWindow()和getLocatio

getLocationInWindow()和getLocatio

作者: code希必地 | 来源:发表于2020-05-26 11:55 被阅读0次

    1、概念

    先看张图:


    c2cec3fdfc039245c9717fb98494a4c27c1e25bb.png

    对比上图,看下这两个函数的定义:

    • getLocationInWindow():控件相对于父窗口(非父布局)的左上角为原点的坐标。
    • getLocationOnScreen():控件相对于屏幕的左上角为原点的坐标。

    2、实例

    2.1、在页面中,非弹窗窗口中

    布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="100dp"
            android:orientation="horizontal"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent">
            <Button
                android:id="@+id/btn"
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:text="显示窗口控件位置"
                android:textSize="20sp" />
            <TextView
                android:id="@+id/tv"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginStart="20dp"
                android:textSize="15sp" />
        </LinearLayout>
    </android.support.constraint.ConstraintLayout>
    

    MainActivity.java

    public class MainActivity extends AppCompatActivity {
        int[] screenLocation = new int[2];
        int[] windowLocation = new int[2];
        private Button button;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_fullscreen);
            button = findViewById(R.id.btn);
            final TextView tv_timer = findViewById(R.id.tv);
            button.post(new Runnable() {
                @Override
                public void run() {
                    //以屏幕的左上角为原点
                    button.getLocationOnScreen(screenLocation);
    
                    //以父窗口(非父布局)的左上角为原点
                    button.getLocationInWindow(windowLocation);
                    tv_timer.setText("dialog_screenX::" + screenLocation[0] + "   dialog_screenY::" + screenLocation[1] + "\n" +
                            "dialog_windowX::" + windowLocation[0] + "   dialog_windowY::" + windowLocation[1]);
                    Log.e("TAG", "screenX::" + screenLocation[0] + "   screenY::" + screenLocation[1]);
                    Log.e("TAG", "windowX::" + windowLocation[0] + "   windowY::" + windowLocation[1]);
                }
            });
      }
    }
    

    效果如下:


    非弹窗中.png

    从图中可以看出,

    • **getLocationInWindow()是相对于父窗口而非父布局的;
    • getLocationOnScreen()是相对于手机屏幕左上角;
    • getLocationInWindow()和getLocationOnScreen()在非弹窗中通常情况下是一致的。

    2.2、在弹窗中

    布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary">
    
        <Button
            android:id="@+id/btn"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:text="弹窗内显示窗口控件位置"
            android:textSize="20sp" />
    
        <TextView
            android:id="@+id/tv"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginStart="20dp"
            android:textColor="@android:color/white"
            android:textSize="15sp"
            app:layout_constraintBottom_toBottomOf="@id/btn"
            app:layout_constraintLeft_toRightOf="@id/btn"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.constraint.ConstraintLayout>
    

    AlertDialog的展示

      AlertDialog.Builder builder = new AlertDialog.Builder(FullScreenActivity.this);
                    View contentView = LayoutInflater.from(FullScreenActivity.this).inflate(R.layout.dialog_layout, null);
                    final Button button = contentView.findViewById(R.id.btn);
                    final TextView tv_timer = contentView.findViewById(R.id.tv);
                    button.post(new Runnable() {
                        @Override
                        public void run() {
                            //以屏幕的左上角为原点
                            button.getLocationOnScreen(screenLocation);
    
                            //以父窗口(非父布局)的左上角为原点
                            button.getLocationInWindow(windowLocation);
                            tv_timer.setText("dialog_screenX::" + screenLocation[0] + "   dialog_screenY::" + screenLocation[1] + "\n" +
                                    "dialog_windowX::" + windowLocation[0] + "   dialog_windowY::" + windowLocation[1]);
                            Log.e("TAG", "dialog_screenX::" + screenLocation[0] + "   dialog_screenY::" + screenLocation[1]);
                            Log.e("TAG", "dialog_windowX::" + windowLocation[0] + "   dialog_windowY::" + windowLocation[1]);
                        }
                    });
                    builder.setView(contentView);
                    builder.show();
    

    效果图


    弹窗中.png

    从图中可以看出,在弹窗中这两个方法的含义:

    • getLocationOnScreen()依然是相对于手机屏幕左上角;
    • **而getLocationInWindow()是相对于父窗口的左上角,此时父窗口不再是页面的窗口了,而是Dialog的窗口,故和getLocationOnScreen()不再一致;

    相关文章

      网友评论

          本文标题:getLocationInWindow()和getLocatio

          本文链接:https://www.haomeiwen.com/subject/icpmahtx.html