Xamarin.Forms: Change Icon & Text size in TabbedPage tabs

前端 未结 3 1875
囚心锁ツ
囚心锁ツ 2021-01-12 17:31

I am developing Tabs using TabbedPage in xaml. The default tabs Icons & Text size is big so I need to reduce the size of Icons & Text.

3条回答
  •  [愿得一人]
    2021-01-12 18:04

    After some effort i get it work for android using TabbedPageRenderer. Created custom layout with ImageView & TetxtView below Custom_tab_layou.xaml

    
    
        
        
    
    

    Created MyTabbedPageRenderer class

        public class MyTabbedPageRenderer : TabbedPageRenderer
        {
            private Dictionary icons = new Dictionary();
            bool setup;
            ViewPager pager;
            TabLayout layout;
            public MyTabbedPageRenderer(Context context) : base(context)
            {
            }
    
            protected override void SetTabIcon(TabLayout.Tab tab, FileImageSource icon)
            {
                base.SetTabIcon(tab, icon);
                tab.SetCustomView(Resource.Layout.Custom_tab_layou);
    
                var imageview = tab.CustomView.FindViewById(Resource.Id.icon);
                var tv = tab.CustomView.FindViewById(Resource.Id.tv);
    
                tv.SetText(tab.Text, TextView.BufferType.Normal);
                imageview.SetBackgroundDrawable(tab.Icon);
    
                ColorStateList colors2 = null;
    
                if ((int)Build.VERSION.SdkInt >= 23)
                    colors2 = Resources.GetColorStateList(Resource.Color.icon_tab, Forms.Context.Theme);
                else
                    colors2 = Resources.GetColorStateList(Resource.Color.icon_tab);
                tv.SetTextColor(colors2);
            }
    
            //this is for changing text color of select tab
            protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
            {
                base.OnElementPropertyChanged(sender, e);
                if (setup)
                    return;
                if (e.PropertyName == "Renderer")
                {
                    pager = (ViewPager)ViewGroup.GetChildAt(0);
                    layout = (TabLayout)ViewGroup.GetChildAt(1);
                    setup = true;
                    ColorStateList colors = null;
    
                    if ((int)Build.VERSION.SdkInt >= 23)
                        colors = Resources.GetColorStateList(Resource.Color.icon_tab, Forms.Context.Theme);
                    else
                        colors = Resources.GetColorStateList(Resource.Color.icon_tab);
    
                    for (int i = 0; i < layout.TabCount; i++)
                    {
                        var tab = layout.GetTabAt(i);
                        var icon = tab.Icon;
    
                        Android.Views.View view = GetChildAt(i);
                        if (view is TabLayout) layout = (TabLayout)view;
    
                        if (icon != null)
                        {
                            icon = Android.Support.V4.Graphics.Drawable.DrawableCompat.Wrap(icon);
                            Android.Support.V4.Graphics.Drawable.DrawableCompat.SetTintList(icon, colors);
                        }
                    }
                }
            }
      }
    

提交回复
热议问题