【问题标题】:Xamarin.Android NavigationTabStripXamarin.Android NavigationTabStrip
【发布时间】:2017-12-29 03:07:34
【问题描述】:

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

【问题讨论】:

    标签: xamarin.android android-tablayout


    【解决方案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;
            }
        }
    }
    

    【讨论】:

    • 有没有办法在材料设计中做第一个? PS谢谢! :D
    • 这实际上会默认你的主题,你不必担心
    • 对不起,在 fragment_sample.axml 文件中,我收到 SlidingTabLayoutTutorial.SlidingTabScrollView 的错误,它不存在..我要疯了..
    • 先生,您使用的是哪个链接?
    • 而且..也许我错了,但它没有主题,我在清单中设置了一个材质主题,但它不起作用:\
    【解决方案2】:

    【讨论】:

    • 谢谢,但我没有使用 Xamarin.Forms..我可以使用第一个吗?
    猜你喜欢
    • 2021-02-14
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    相关资源
    最近更新 更多