getRight,getLeft,getTop返回零

use*_*390 9 android

我正在使用以下代码.但是所有方法都返回零值.我知道要获取视图的坐标,我们应该绘制视图.这就是为什么我在onResume方法中使用代码但仍然无法正常工作.任何的想法?

 @Override
public void onResume(){
    super.onResume();
    System.out.println("Onresume"); 
    System.out.println("tab1 - left" + btn_Tab7 .getLeft());
    System.out.println("tab1 - Top" + btn_Tab7.getTop());
    System.out.println("tab1 - right" + btn_Tab7.getRight());
    System.out.println("tab1 - bottom" + btn_Tab7.getBottom()); 
}
Run Code Online (Sandbox Code Playgroud)

vik*_*kki 8

onResume为时太早,无法调用getLeft,getRight ......执行它onWindowFocusChanged

@Override
public void onWindowFocusChanged (boolean hasFocus){
    super.onWindowFocusChanged(hasFocus);
    if(hasFocus){
        System.out.println("onWindowFocusChanged"); 
        System.out.println("tab1 - left" + btn_Tab7 .getLeft());
        System.out.println("tab1 - Top" + btn_Tab7.getTop());
        System.out.println("tab1 - right" + btn_Tab7.getRight());
        System.out.println("tab1 - bottom" + btn_Tab7.getBottom());
    }
}
Run Code Online (Sandbox Code Playgroud)

来自onResume的文档:

请记住,onResume不是您的活动对用户可见的最佳指标 ; 诸如键盘锁之类的系统窗口可以在前面.使用onWindowFocusChanged(boolean)可以确定您的活动对用户可见(例如,恢复游戏).