getLocationOnScreen不起作用原因

要在窗口获得焦点以后再去获取控件在屏幕上的绝对坐标,在onCreate或者onResume函数里使用,均得不到实际坐标。

解决方法

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="leftTop"
    android:id="@+id/textView"/>

使用配置属性方法点击回调

    public void leftTop(View view) {
 
int[] location = new int[2];
view.getLocationOnScreen(location);
    }

猜你喜欢

转载自blog.csdn.net/meixi_android/article/details/85051054
今日推荐