0

在 OnCreate Button.getLeft 返回 0。Button 是动态创建的,并且 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?

    }
4

1 回答 1

2

根据这个问题的答案,您将无法在 Activity 实例化期间检索 View 对象的 getLeft() 。对于我链接的情况,他谈到了OnResume但由于OnCreate发生在OnResume 生命周期之前,您将看到类似的结果。

如需解决方案,请拨打您的msg()电话并将其转至onWindowFocusChanged

于 2013-04-05T19:09:18.187 回答