在 OnCreate 中,Button.getLeft 返回 0。按钮是动态创建的,Button.performClick() 工作正常。怎么离开OnCreate? OnCreate之后我可以获得真正的左值。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
create_buttons();
final Button bt = (Button) findViewById(Active);
bt.performClick();
msg(bt.getLeft()); //returns 0, why?
}
最佳答案
根据 this question 的回答在 Activity 实例化期间,您将无法检索 View 对象的 getLeft() 。对于我链接的案例,他谈到了 OnResume
,但由于 OnCreate
发生在 OnResume
之前 in the lifecycle您将看到类似的结果。
要获得解决方案,请调用 msg()
并将其放到 onWindowFocusChanged
关于java - 在 OnCreate Button.getLeft 返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15841517/