如何在Android xamrin中的OnCreate方法中调用Main.axml时动态创建片段并在片段中显示数据。
请帮助
提前致谢
答案 0 :(得分:0)
您可以为您的片段动态创建FrameLayout
。例如:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.Main);
LinearLayout root = FindViewById<LinearLayout>(Resource.Id.rootlayout);
FrameLayout frame = new FrameLayout(this);
frame.Id = 10001000;
frame.LayoutParameters = new ViewGroup.LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent);
root.AddView(frame);
if (savedInstanceState == null)
{
Fragment fragment = new MyFragment();
FragmentTransaction ft = FragmentManager.BeginTransaction();
ft.Add(frame.Id, fragment).Commit();
}
}
当然,我们还需要创建自定义Fragment
,例如:
public class MyFragment : Fragment
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
//Create your view and add your data here. Return this view.
//For example:
TextView tv = new EditText(this.Activity);
tv.Text = "This is Fragment";
return tv;
}
}