Android 在活动中何时绘制约束图?
我有一个带有约束的Android活动。我想通过编程获得ConstraintLayout的不同视图的高度和宽度,但是我得到了0,因为我是在onCreate()中完成的,我想布局还没有绘制出来 我应该在哪里做 提前感谢Android 在活动中何时绘制约束图?,android,android-layout,android-constraintlayout,activity-lifecycle,Android,Android Layout,Android Constraintlayout,Activity Lifecycle,我有一个带有约束的Android活动。我想通过编程获得ConstraintLayout的不同视图的高度和宽度,但是我得到了0,因为我是在onCreate()中完成的,我想布局还没有绘制出来 我应该在哪里做 提前感谢 int height; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layou
int height;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
final View myView = findViewById(R.id.myView);
myView.post(new Runnable(){
@Override
public void run() {
//do the operations related to height here
//this method is asynchronous
height = myView.getHeight();
Log.d(TAG, "Height inside runnable is : "+height); //this will be correct height
}
});
Log.d(TAG, "Height is : "+height); //this will be zero because above method is asynchronous
}
使用post操作可以确保在视图创建完成后调用其中的代码