2014-04-08 22 views
0

需要从另一个类Xamarin添加Android父视图中的视图组件。以下方式尝试过,但无法在没有上下文的情况下创建视图组件。如何添加一个视图到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

如果我理解你正确地更换

homeScreenTextView = new TextView (this); 

homeScreenTextView = new TextView (parentView.getContext()); 
+0

[http://stackoverflow.com/questions/22946999/如何-可以-I-附加一个视图到一个交流vity-from-another-class-in-android/22947204?noredirect = 1#22947204]“+ 1”谢谢@ABFORCE! – ashik

+0

@ashik如果您发现我的答案有帮助,请将其视为已接受的答案! –

相关问题