Xamarin.Android NavigationTabStrip

问题描述 投票:2回答:2

我想在我的应用程序中实现NavigationTabStrip(https://github.com/martijn00/NavigationTabStripXamarin),但我不知道如何。我也看过原来的项目,但我是这个世界的新人。有没有人帮我实现这个项目?或者是否有人知道如何做到这一点?我正在尝试在材料设计中创建一个TabLayout。

xamarin.android android-tablayout
2个回答
1
投票

最简单的方法是这样的:

http://camposha.info/source/xamarin-android-swipe-tabs-viewpager-fragments-images-actionbartabs/

http://www.c-sharpcorner.com/article/creating-sliding-tab-layout-interface-using-xamarin-android-using-visual-studio/

代码在xamarin中,因此对您来说很容易实现!

祝好运!

更新:

您的活动代码Axml文件:

 <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.v7.widget.Toolbar
  android:id="@+id/toolbar"
  android:layout_width="match_parent"
  android:layout_height="?actionBarSize"
  android:minHeight="?attr/actionBarSize"
  android:background="?attr/colorPrimary"
  android:theme="@style/MyTheme"
  app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

 <android.support.design.widget.TabLayout
  android:id="@+id/tabs"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  app:tabGravity="fill"
  app:tabMode="fixed"
   />
<FrameLayout
  android:id="@+id/frame_container"
  android:layout_width="match_parent"
  android:layout_height="match_parent" />
</LinearLayout>

您的活动代码cs文件:

 public class GoogleMapsActivity : AppCompatActivity
{
    private Fragment MapsFragment;
    private Fragment ListFragment;
    private TabLayout allTabs;
    FragmentManager fm;
    FragmentTransaction ft;
  protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.your_activity);
        bindAllWidgets();
        setupTabLayout();
        setUpToolbar();
    }
  void setUpToolbar()
    {
        #region "Tool bar"
        var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
        toolbar.SetTitleTextColor(ResourcesCompat.GetColor(Resources, Resource.Color.colorRedText, null));
        Drawable upArrow = ResourcesCompat.GetDrawable(Resources, Resource.Drawable.abc_ic_ab_back_mtrl_am_alpha, null);
        upArrow.SetColorFilter(Color.ParseColor(Resources.GetString(Resource.Color.colorRedText)), PorterDuff.Mode.SrcAtop);
        SetSupportActionBar(toolbar);
        SupportActionBar.Title = Resources.GetString(Resource.String.toolbar_title_dhl_service_points);
        SupportActionBar.SetHomeButtonEnabled(true);
        SupportActionBar.SetDisplayHomeAsUpEnabled(true);
        SupportActionBar.SetHomeAsUpIndicator(upArrow);
        #endregion
    }
    private void setupTabLayout()
    {
        allTabs.AddTab(allTabs.NewTab().SetText(Resources.GetString(Resource.String.google_maps_page_title_map)), true);
        allTabs.AddTab(allTabs.NewTab().SetText(Resources.GetString(Resource.String.google_maps_page_title_list)));
    }

    private void bindAllWidgets()
    {
        allTabs = FindViewById<TabLayout>(Resource.Id.tabs);
        allTabs.TabSelected += AllTabs_TabSelected;
    }
    private void AllTabs_TabSelected(object sender, TabSelectedEventArgs e)
    {
        var tab = e.Tab;
        setCurrentTabFragment(tab.Position);
    }
    public void replaceFragment(Fragment fragment)
    {
        fm = FragmentManager;
        ft = fm.BeginTransaction();
        if (fragment == MapsFragment)
        {
            if (MapsFragment == null)
            {
                MapsFragment = new PoSLocationMapView(this);
                ft.Add(Resource.Id.frame_container, MapsFragment, "MapsFragment");

            }
            else
            {
                ft.Detach(FragmentManager.FindFragmentByTag("ListFragment"));
                ft.Attach(MapsFragment);
            }
        }
        else if (fragment == ListFragment)
        {
            if (ListFragment == null)
            {
                ListFragment = new ServicePointDHLList(this);
                ft.Add(Resource.Id.frame_container, ListFragment, "ListFragment");
            }
            else
            {
                ft.Detach(FragmentManager.FindFragmentByTag("MapsFragment"));
                ft.Attach(ListFragment);
            }
        }
        ft.SetTransition(FragmentTransit.FragmentOpen);
        ft.Commit();

    }
    private void setCurrentTabFragment(int tabPosition)
    {
        switch (tabPosition)
        {
            case 0:
                replaceFragment(MapsFragment);
                break;
            case 1:
                replaceFragment(ListFragment);
                break;
        }
    }
}

© www.soinside.com 2019 - 2024. All rights reserved.