Android中恰当的自定义视图构造

Android中恰当的自定义视图构造,android,Android,我想知道,从自定义视图的构造函数中膨胀子视图是否被认为是一种不好的做法,如果是的话,可能是这样做的原因 public class MyLinearLayout : LinearLayout { public MyLinearLayout (Context context, IAttributeSet attributes) : base(context, attributes) { LayoutInflater inflater = (LayoutInfla

我想知道,从自定义视图的构造函数中膨胀子视图是否被认为是一种不好的做法,如果是的话,可能是这样做的原因

public class MyLinearLayout : LinearLayout
{ 
    public MyLinearLayout (Context context, IAttributeSet attributes) : base(context, attributes) 
    { 
        LayoutInflater inflater = (LayoutInflater)context.ApplicationContext.GetSystemService(Context.LayoutInflaterService);

        View child = inflater.Inflate(Resource.Layout.ChildView, null);

        this.AddView(child);
    }
}

public class MyLinearLayout : LinearLayout
{ 
    public MyLinearLayout (Context context, IAttributeSet attributes) : base(context, attributes) 
    { 
        View child = new ChildView(context);
        child.LayoutParameters = new LayoutParameters(...);

        this.AddView(child);
    }
}

谢谢。

这种方法没有问题,因为它可以在
NumberPicker
之类的类中找到。如果使用
LayoutInflater.inflate(int,ViewGroup,boolean)
方法附加到根视图,则可以使用
Merge
优化。例如


View child=充气机。充气(R.layout.ChildView,this,true)

这种形式的调用将利用合并和包含标记?!不确定是否包括在内,但我不明白为什么不包括在内。如果你发现了,请在这里更新。