需要从另一个类Xamarin在android父布局中添加视图组件。以下方式尝试但无法在没有上下文的情况下创建视图组件。
/// <summary>
/// Second activity is intended to have a layout which will populated by another class
/// </summary>
public class SecondActivity : Activity
{
private LinearLayout homeScreenLinearLayout;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Create your application here
homeScreenLinearLayout = new LinearLayout(this);
homeScreenLinearLayout.Orientation = Orientation.Vertical;
AddCompeonent ObjAddComponent = new AddCompeonent ();
//The method returns the populated view
View populatedParentView = ObjAddComponent.AddedView (homeScreenLinearLayout);
//Show the view generated by add component class
SetContentView(homeScreenLinearLayout);
}
}
}
public class AddCompeonent
{
private TextView homeScreenTextView;
public View AddedView(View parentView){
LinearLayout homeLayout =(LinearLayout) parentView;
//Create textview here
homeScreenTextView = new TextView (this);
homeScreenTextView.Text = "here you will load home screen";
//Add it to the layout
homeLayout.AddView (homeScreenTextView);
return homeLayout;
}
}
答案 0 :(得分:0)
如果我理解你正确替换
homeScreenTextView = new TextView (this);
与
homeScreenTextView = new TextView (parentView.getContext());