step3:Fragment布局文件继承Fragment的类MyFragment
fg_content.xml:
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bg_white">
android:id="@+id/txt_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="呵呵"
android:textColor="@color/color_primary"
android:textSize="20sp" />
MyFragment.cs
public class MyFragment : Fragment
{
private string content { get; set; }
public MyFragment(string content)
{
this.content = content;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.Inflate(Resource.Layout.fg_content, container, false);
TextView txt_content = (TextView)view.FindViewById(Resource.Id.txt_content);
txt_content.Text = content;
return view;
}
}
step3:MainActivity.cs
[Activity(Label = "FragmentDemo", MainLauncher = true, Icon = "@drawable/icon", Theme = "@android:style/Theme.Light.NoTitleBar")]
public class MainActivity : Activity
{
private TextView txt_chat;
private TextView txt_contacts;
private TextView txt_more;
private FrameLayout ly_content;
private MyFragment fg1, fg2, fg3;
private FragmentManager fManager;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
ly_content = (FrameLayout)FindViewById(Resource.Id.ly_content);
MyFragment fg = new MyFragment("第一个fragment");
txt_chat = (TextView)FindViewById(Resource.Id.txt_chat);
txt_contacts = (TextView)FindViewById(Resource.Id.txt_contacts);
txt_more = (TextView)FindViewById(Resource.Id.txt_more);
bindViews();
txt_chat.PerformClick();
}
//ui组件初始化与事件绑定
private void bindViews()
{
txt_chat.Click += (s, e) => { onClick(txt_chat); };
txt_contacts.Click += delegate { onClick(txt_contacts); };
txt_more.Click += delegate { onClick(txt_more); };
}
//隐藏所有Fragment
private void hideAllFragment(FragmentTransaction fragmentTransaction)
{
if (fg1 != null) fragmentTransaction.Hide(fg1);
if (fg2 != null) fragmentTransaction.Hide(fg2);
if (fg3 != null) fragmentTransaction.Hide(fg3);
}
//重置所有文本的选中状态
private void setSelected()
{
txt_chat.Selected =false;
txt_contacts.Selected = false;
txt_more.Selected = false;
}
//单击事件
public void onClick(View v)
{
FragmentTransaction fTransaction = FragmentManager.BeginTransaction();
hideAllFragment(fTransaction);
switch (v.Id)
{
case Resource.Id.txt_chat:
setSelected();
txt_chat.Selected = true;
if (fg1 == null)
{
fg1 = new MyFragment("聊天Fragment");
fTransaction.Add(Resource.Id.ly_content, fg1);
}
else{fTransaction.Show(fg1);}break;
case Resource.Id.txt_contacts:
setSelected();
txt_contacts.Selected = true;
if (fg2 == null)
{
fg2 = new MyFragment("联系人Fragment");
fTransaction.Add(Resource.Id.ly_content, fg2);
}
else{fTransaction.Show(fg2);}
break;
case Resource.Id.txt_more:
setSelected();
txt_more.Selected = true;
if (fg3 == null)
{
fg3 = new MyFragment("MoreFragment");
fTransaction.Add(Resource.Id.ly_content, fg3);
}else{fTransaction.Show(fg3);}break;
}
fTransaction.Commit();
}
}
关于继承的主题使用的android自带的主题Theme.Light.NoTitle,当然你也可以引入v7兼容包,继承AppcompatActivity,使用兼容包主题