Xamarin.Forms:更改图标 &TabbedPage 选项卡中的文本大小 [英] Xamarin.Forms: Change Icon & Text size in TabbedPage tabs

查看:56
本文介绍了Xamarin.Forms:更改图标 &TabbedPage 选项卡中的文本大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 xaml 中的 TabbedPage 开发标签.默认标签 图标 &文字尺寸很大所以我需要减小图标的尺寸&文本.下面是我设置图标的 main.xaml 代码.

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. Below is my main.xaml code Where I am setting icons.

<TabbedPage  xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"              
             xmlns:local="clr-namespace:TabbedApp">
    <local:DairyTabs Icon="dairy" HeightRequest="10" WidthRequest="10" ></local:DairyTabs>
    <local:Mykid Icon="kid" ></local:Mykid>
    <local:Event Icon="about"></local:Event>    
</TabbedPage>

这是标签的第一页,我将标签的标题设为 Title="Dairy"

This is First page of tabs where I am giving Title of tab as Title="Dairy"

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"              
             Title="Dairy"> 
    <ContentPage.Content>
        <StackLayout>
            <Button x:Name="btnDemo" Text="Go for 2nd page"></Button>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

请参阅下面的屏幕截图,您可以在其中看到图标和标签文本.

See the below Screenshot where you can see icons and tab text.

推荐答案

经过一番努力,我使用 TabbedPageRenderer 使其适用于 android.使用 ImageView 创建自定义布局 &TetxtView 低于 Custom_tab_layou.xaml

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="41dp"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/icon"
        android:layout_width="18dp"
        android:layout_height="18dp"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="4dp" />
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="hello"
        android:gravity="center"
        android:textSize="11dp"
        android:textColor="#00FF6F" />
</LinearLayout>

创建MyTabbedPageRenderer

    public class MyTabbedPageRenderer : TabbedPageRenderer
    {
        private Dictionary<Int32, Int32> icons = new Dictionary<Int32, Int32>();
        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<ImageView>(Resource.Id.icon);
            var tv = tab.CustomView.FindViewById<TextView>(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);
                    }
                }
            }
        }
  }

这篇关于Xamarin.Forms:更改图标 &amp;TabbedPage 选项卡中的文本大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆