美文网首页
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

    1、概念 先看张图: 对比上图,看下这两个函数的定义: getLocationInWindow():控件相对于父窗...

  • getLocationInWindow和getLocationO

    1.获取屏幕宽高,获取到的高度为包括ActionBar和StatusBar的屏幕高度,但不包括底部导航栏,可以选择...

  • View类中相关方法记录

    测量相关的工具方法 获取位置相关方法 getLocationInWindow(int[] pos): Compu...

  • 你真的理解getLocationInWindow了吗?

    近期项目比较忙,今日才有时间给大家分享android的一些实用的知识~ what is getLocationIn...

  • -和 和 -

    产品介绍:和和是一款会员制共享平台;所有 经营者可在APP内注册和和商家成为会员供 应商(实体店、网店、微商、平台...

  • &和&&,|和||

    原文:https://blog.csdn.net/chinabestchina/article/details/7...

  • 和可和,非常和

    我年纪很小的时候,父亲有一本笔记本,上面只写了一句话:万物并育而不相害,道并行而不相悖。我当时很喜欢这句话,所以期...

  • kotlin中的空? 和 ?. 和 ?: 和 as? 和 !!

    ? 可空类型 kotlin和Java的类型系统之间的一个很重要的区别就是,Kotlin对可空类型的显示支持 也就是...

  • self. 和 _ 和 = 和 set

    声明了一个属性 @property (a,b) p1; 只有用self.调用时修饰关键词才起作用, 用_调用...

  • Observable和Observe和Subcriblers 和

    Observable事件源,被观察者。Subcriblers 观察者,事件订阅者Observer 同Subcrib...

网友评论

      本文标题:getLocationInWindow()和getLocatio

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