我想以编程方式将一些图形添加到布局中。我想根据xml layout file
.
onCreateView
如果我尝试在或 in 中访问它们,布局中图形的位置都是 0 onViewCreated
。这可能是因为布局还没有完成膨胀。
我在哪里可以连接我的代码以确保片段的布局已经完成膨胀?
目前我已经使用viewTreeObserver
(见下面的代码)解决了它,但这真的是最聪明的方法吗?
我正在一个片段内工作。
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.myfrg, container, false);
mMainLayoutCanvas = (RelativeLayout)v.findViewById(R.id.myfrg_mainlayout);
mMainLayoutCanvas.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
initializeUI();
}
});
return v;
}