完整代码:Google Drive。
我使用准备好的模板“导航抽屉”创建,如下图所示:
关注代码:
Fragment2.cs
public class Fragment2 : Fragment
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public static Fragment2 NewInstance()
{
var frag2 = new Fragment2 { Arguments = new Bundle() };
return frag2;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var ignored = base.OnCreateView(inflater, container, savedInstanceState);
return inflater.Inflate(Resource.Layout.fragment2, null);
}
}
fragment2.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="@string/fragment2"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/textView1"
android:gravity="center" />
</LinearLayout>
main.axml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- The main content view -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/toolbar_layout">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"
app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/content_frame"
android:layout_below="@id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header"
app:menu="@menu/nav_menu"
android:fitsSystemWindows="true" />
</android.support.v4.widget.DrawerLayout>
MainActivity.cs
Android.Support.V4.App.Fragment fragment = null;
switch (position)
{
case 0:
fragment = Fragment1.NewInstance();
break;
case 1:
fragment = Fragment2.NewInstance();
break;
}
如何在片段中放置2个标签?与下图相同:
我创建了一个导航抽屉,每个菜单都有它的片段,这个片段有2个标签。我需要帮助,比如在xamarin C#中添加两个标签。
如何将2个标签放入我的片段中的任何解决方案?
答案 0 :(得分:3)
Fragment1
:
using Android.OS;
using Android.Support.Design.Widget;
using Android.Support.V4.App;
using Android.Support.V4.View;
using Android.Views;
using Java.Lang;
using System.Collections.Generic;
namespace NavigationDrawerAndTabs.Fragments
{
public class Fragment1 : Fragment
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public static Fragment1 NewInstance()
{
var frag1 = new Fragment1 { Arguments = new Bundle() };
return frag1;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View inflate = inflater.Inflate(Resource.Layout.fragment1, container, false);
ViewPager viewPager = inflate.FindViewById<ViewPager>(Resource.Id.viewPager);
MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(ChildFragmentManager);
viewPager.Adapter = adapter;
//TabLayout
TabLayout tabLayout = inflate.FindViewById<TabLayout>(Resource.Id.sliding_tabs);
tabLayout.SetupWithViewPager(viewPager);
return inflate;
}
public class MyFragmentPagerAdapter : FragmentPagerAdapter
{
public string[] titles = new string[] { "Tab1", "Tab2" };
List<Fragment> fgls = new List<Fragment>();
public MyFragmentPagerAdapter(Android.Support.V4.App.FragmentManager fm)
: base(fm)
{
fgls.Add(Fragment2.NewInstance());
fgls.Add(Fragment3.NewInstance());
}
public override Android.Support.V4.App.Fragment GetItem(int position)
{
return fgls[position];
}
public override ICharSequence GetPageTitleFormatted(int position)
{
return new Java.Lang.String(titles[position]);
}
public override int Count
{
get { return titles.Length; }
}
}
}
}
Fragment1's layout
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>
Fragment2
:
using Android.OS;
using Android.Support.V4.App;
using Android.Views;
namespace NavigationDrawerAndTabs.Fragments
{
public class Fragment2 : Android.Support.V4.App.Fragment
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public static Fragment2 NewInstance()
{
var frag2 = new Fragment2 { Arguments = new Bundle() };
return frag2;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.Inflate(Resource.Layout.fragment2, container, false);
}
}
}
Fragment3
:
using Android.OS;
using Android.Support.V4.App;
using Android.Views;
namespace NavigationDrawerAndTabs.Fragments
{
public class Fragment3 : Fragment
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public static Fragment3 NewInstance()
{
var frag3 = new Fragment3 { Arguments = new Bundle() };
return frag3;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.Inflate(Resource.Layout.fragment3, container, false);
}
}
}