10

我正在使用以下代码。但是所有方法都返回零值。我知道要获得视图的坐标,我们应该绘制视图。这就是为什么我在 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()); 
}
4

1 回答 1

9

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());
    }
}

来自 onResume 的文档:

请记住,onResume 并不是您的活动对用户可见的最佳指标;系统窗口(例如键盘保护)可能位于前面。使用 onWindowFocusChanged(boolean) 确定您的活动对用户可见(例如,恢复游戏)。

于 2012-08-21T10:36:32.503 回答