2017-12-17 10 views
0

페이지 수가 많은 탭 페이지가 있습니다. 페이지 제목이 잘 보이지 않아서 텍스트를 감싸거나 확장 할 필요가 없습니다. 이 문제를 해결하기위한 제안. enter image description herexamarin forms 탭 페이지 제목 없음 랩

+0

문제를 일으킬 수있는 코드 스 니펫을 공유하면 도움이 될 수 있습니다. 또한 스크린 샷이 너무 작아서 여기에서 문제를 볼 수 없습니다. – Kuba

답변

1

당신은 당신은 CustomMain 페이지

 using System; 
     using System.Collections.Generic; 
     using System.Linq; 
     using System.Text; 

     using Xamarin.Forms; 

     namespace AppTab 
     { 
      public class CustomMain : CustomTabbedPage 
      { 
       public CustomMain() 
       { 

        BarBackgroundApplyTo = BarBackgroundApplyTo.Android; 

        //BarBackgroundColor = Color.Red; 
        Children.Add(new abc() { Title = "ABC"}); 
        Children.Add(new xyz() { Title = "XYZ" }); 
        Children.Add(new pqr() { Title = "PQR" }); 
        Children.Add(new str() { Title = "STR" }); 
        Children.Add(new ftx() { Title = "FTX" }); 
        Children.Add(new dtb() { Title = "DTB" }); 
       } 
      } 
     } 

Click here for ScreenShot

필요는 CustomTabbedPage 클래스

  using System; 
      using System.Collections.Generic; 
      using System.Linq; 
      using System.Text; 
      using System.Threading.Tasks; 
      using Xamarin.Forms; 

      namespace AppTab 
      { 


       [Flags] 
       public enum BarBackgroundApplyTo 
       { 
        None = 0x01, 
        Android = 0x10, 
        iOS = 0x100 
       } 
       public class CustomTabbedPage : TabbedPage 
       { 
        public CustomTabbedPage() 
        { 

        } 


        public static readonly BindableProperty SelectedColorProperty = 
         BindableProperty.Create(nameof(SelectedColor), typeof(Color), typeof(CustomTabbedPage), default(Color)); 

        public Color SelectedColor 
        { 
         get => (Color)GetValue(SelectedColorProperty); 
         set => SetValue(SelectedColorProperty, value); 
        } 

        public static readonly BindableProperty BarBackgroundApplyToProperty = 
         BindableProperty.Create(nameof(BarBackgroundApplyTo), typeof(BarBackgroundApplyTo), typeof(CustomTabbedPage), BarBackgroundApplyTo.Android); 

        public BarBackgroundApplyTo BarBackgroundApplyTo 
        { 
         get => (BarBackgroundApplyTo)GetValue(BarBackgroundApplyToProperty); 
         set => SetValue(BarBackgroundApplyToProperty, value); 
        } 

        public new static readonly BindableProperty BarBackgroundColorProperty = 
         BindableProperty.Create(nameof(BarBackgroundColor), typeof(Color), typeof(CustomTabbedPage), default(Color)); 

        public new Color BarBackgroundColor 
        { 
         get => (Color)GetValue(BarBackgroundColorProperty); 
         set => SetValue(BarBackgroundColorProperty, value); 
        } 


       } 
      } 

그리고 세 번째가 필요 하나 TabbedPageRenderer에게

 using System; 
     using System.Collections.Generic; 
     using System.Linq; 
     using System.Text; 

     using Android.App; 
     using Android.Content; 
     using Android.OS; 
     using Android.Runtime; 
     using Android.Views; 
     using Android.Widget; 
     using Xamarin.Forms; 
     using Xamarin.Forms.Platform.Android.AppCompat; 
     using AppTab.Droid; 
     using Android.Support.V4.View; 
     using Android.Support.Design.Widget; 
     using System.ComponentModel; 
     using Xamarin.Forms.Platform.Android; 
     using AppTab; 
     using Android.Graphics.Drawables; 
     using Android.Graphics; 

     [assembly: ExportRenderer(typeof(CustomTabbedPage), typeof(ScrollableTabbedPage))] 
     namespace AppTab.Droid 
     { 
      public class ScrollableTabbedPage : TabbedPageRenderer 
      { 
       public override void OnViewAdded(Android.Views.View child) 
       { 
        base.OnViewAdded(child); 
        var tabLayout = child as TabLayout; 
        if (tabLayout != null) 
        { 
         tabLayout.TabMode = TabLayout.ModeScrollable; 
        } 
       } 


       public static void Init() 
       { 
        var unused = DateTime.UtcNow; 
       } 

       private CustomTabbedPage FormsTabbedPage => Element as CustomTabbedPage; 
       private Android.Graphics.Color _selectedColor = Android.Graphics.Color.Black; 
       private static readonly Android.Graphics.Color DefaultUnselectedColor = Xamarin.Forms.Color.Gray.ToAndroid(); 
       private static Android.Graphics.Color _barBackgroundDefault; 
       private Android.Graphics.Color _unselectedColor = DefaultUnselectedColor; 

       ViewPager _viewPager; 
       TabLayout _tabLayout; 

       protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e) 
       { 

        base.OnElementChanged(e); 

        // Get tabs 
        for (var i = 0; i < ChildCount; i++) 
        { 
         var v = GetChildAt(i); 
         var pager = v as ViewPager; 
         if (pager != null) 
          _viewPager = pager; 
         else if (v is TabLayout) 
          _tabLayout = (TabLayout)v; 
        } 


        if (e.OldElement != null) 
        { 
         _tabLayout.TabSelected -= TabLayout_TabSelected; 
         _tabLayout.TabUnselected -= TabLayout_TabUnselected; 
        } 

        if (e.NewElement != null) 
        { 
         _barBackgroundDefault = (_tabLayout.Background as ColorDrawable)?.Color ?? 
          Android.Graphics.Color.Blue; 
         SetSelectedColor(); 
         SetBarBackgroundColor(); 
         _tabLayout.TabSelected += TabLayout_TabSelected; 
         _tabLayout.TabUnselected += TabLayout_TabUnselected; 

         SetupTabColors(); 
         SelectTab(0); 
        } 

       } 

       void SelectTab(int position) 
       { 
        if (_tabLayout.TabCount > position) 
        { 
         _tabLayout.GetTabAt(position).Icon? 
          .SetColorFilter(_selectedColor, PorterDuff.Mode.SrcIn); 
         _tabLayout.GetTabAt(position).Select(); 
        } 
        else 
        { 
         throw new IndexOutOfRangeException(); 
        } 
       } 


       void SetupTabColors() 
       { 
        _tabLayout.SetSelectedTabIndicatorColor(_selectedColor); 
        _tabLayout.SetTabTextColors(_unselectedColor, _selectedColor); 
        for (int i = 0; i < _tabLayout.TabCount; i++) 
        { 
         var tab = _tabLayout.GetTabAt(i); 
         tab.Icon?.SetColorFilter(_unselectedColor, PorterDuff.Mode.SrcIn); 
        } 
       } 

       private void TabLayout_TabUnselected(object sender, TabLayout.TabUnselectedEventArgs e) 
       { 
        var tab = e.Tab; 
        tab.Icon?.SetColorFilter(_unselectedColor, PorterDuff.Mode.SrcIn); 
       } 

       private void TabLayout_TabSelected(object sender, TabLayout.TabSelectedEventArgs e) 
       { 
        var tab = e.Tab; 
        _viewPager.CurrentItem = tab.Position; 
        tab.Icon?.SetColorFilter(_selectedColor, PorterDuff.Mode.SrcIn); 
       } 

       protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) 
       { 
        int lastPosition = _tabLayout.SelectedTabPosition; 
        switch (e.PropertyName) 
        { 
         case nameof(CustomTabbedPage.BarBackgroundColor): 
         case nameof(CustomTabbedPage.BarBackgroundApplyTo): 
          SetBarBackgroundColor(); 
          SetupTabColors(); 
          SelectTab(lastPosition); 
          break; 
         case nameof(CustomTabbedPage.SelectedColor): 
          SetSelectedColor(); 
          SetupTabColors(); 
          SelectTab(lastPosition); 
          break; 
         default: 
          base.OnElementPropertyChanged(sender, e); 
          break; 
        } 
       } 

       private void SetSelectedColor() 
       { 

        if (FormsTabbedPage.SelectedColor != default(Xamarin.Forms.Color)) 
         _selectedColor = FormsTabbedPage.SelectedColor.ToAndroid(); 
       } 

       private void SetBarBackgroundColor() 
       { 
        if (FormsTabbedPage.BarBackgroundApplyTo.HasFlag(BarBackgroundApplyTo.Android)) 
        { 
         _tabLayout.SetBackgroundColor(FormsTabbedPage.BarBackgroundColor.ToAndroid()); 
         _unselectedColor = FormsTabbedPage.BarBackgroundColor != default(Xamarin.Forms.Color) 
          ? FormsTabbedPage.BarBackgroundColor.ToAndroid() 
          : DefaultUnselectedColor; 
        } 
        else 
        { 
         _tabLayout.SetBackgroundColor(_barBackgroundDefault); 
         _unselectedColor = DefaultUnselectedColor; 
        } 
       } 
      } 
     } 

둘째를 작성해야