我有CustomViewGroup,其中包含LinearLayout作为直接子级。按钮被添加为LinearLayout的子级。每当我更改CustomViewGroup的布局时,LinearLayout边界都会不断变化,按钮边界保持为零。
internal class CustomLayout : ViewGroup
{
LinearLayout Linear;
Button button;
internal CustomLayout(Context context):base(context)
{
linear=new LinearLayout(context);
button=new Button(context);
button.Text="ButtonView";
linear.AddView(button);
AddView(Linear);
}
protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
}
}
在将布局值更改为指定值时,按钮的边界保持为零
CustomLayout m_custom=new CustomLayout(this);
m_custom.Layout(0,0,300,300);
答案 0 :(得分:0)
我建议您为按钮布局参数提供以下内容:
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WrapContent,
LinearLayout.LayoutParams.WrapContent);
linearLayoutParams.SetMargins(0, 0, 0, 0);
button.LayoutParameters = linearLayoutParams;
linear.LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent,
LinearLayout.LayoutParams.MatchParent);