Android 膨胀自定义视图的正确位置

Android correct place to inflate custom view

我有这个自定义视图:

class BaseView extends FrameLayout() {
     @Override
     void onAttachedToWindow() {
         View.inflate(context, R.layout.view, this)
     }
}

扩充自定义视图的正确位置是在 onAttachedToWindow 方法中吗?

我在想,也许在onAttach之后对其进行膨胀可以使父布局整个重绘。

如果在onAttachedToWindow中inflate错误,正确的地方在哪里?

此视图是动态添加的(通过 addView),在其他地方它被放置在 xml 定义中。

扩充自定义视图布局的正确位置是在构造函数中:

class BaseView extends FrameLayout {
    public BaseView(Context context) { this(context, null); }

    public BaseView(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view, this, true);

        ...
    }
}