getLocationInWindow()和getLocatio

2020-05-26  本文已影响0人  code希必地

1、概念

先看张图:


c2cec3fdfc039245c9717fb98494a4c27c1e25bb.png

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

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

从图中可以看出,

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

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

上一篇下一篇

猜你喜欢

热点阅读