【问题标题】:Xamarin.Forms: Change Icon & Text size in TabbedPage tabsXamarin.Forms:更改 TabbedPage 选项卡中的图标和文本大小
【发布时间】:2018-08-30 02:26:07
【问题描述】:

我正在使用 xaml 中的 TabbedPage 开发选项卡。默认选项卡图标和文本大小很大,所以我需要减小图标和文本的大小。下面是我的main.xaml 代码我在哪里设置图标。

<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"

<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>

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

【问题讨论】:

标签: xaml xamarin xamarin.forms tabbedpage


【解决方案1】:

在您的 Android 项目的 Resources/values/style.xml 文件中,您可以创建样式:

<style name="MyTabTextStyle" parent="Base.Widget.Design.TabLayout">
    <item name="android:textSize">8sp</item>
</style>

然后在您的Resources/layout/tabs.axml 文件中,您可以使用以下样式:

<android.support.design.widget.TabLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    ... other attributes ...
    app:tabTextAppearance="@style/MyTabTextStyle" />

至于图标,试试这个:https://stackoverflow.com/a/46465233/3183946

顺便说一句,我认为“乳制品”在您的应用中应该是“日记”

【讨论】:

  • 是的,应该是“日记”。我上面的代码main.xaml 存在于 Xamarin.Forms 项目的 PCL 中(常见于 Android、iOS、Windows)。你是说不要使用main.xaml
  • 不,你应该使用main.xaml。但要更改选项卡文本的外观,您必须在您的 Android 项目中更改它,特别是在 tabs.axml 文件中。这是设置 Android 原生选项卡布局的默认样式的地方。不幸的是,您无法在 PCL 项目中自定义选项卡的字体大小
  • 知道了。那么iOS呢?我需要在哪里编写代码,因为我正在为两个操作系统并行执行应用程序。
【解决方案2】:

经过一番努力,我使用TabbedPageRenderer 使它适用于android。在 Custom_tab_layou.xaml

下使用 ImageViewTetxtView 创建了自定义布局
<?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);
                    }
                }
            }
        }
  }
【解决方案3】:

在我的情况下,我真的很想跳过所有自定义渲染器......而且许多实现似乎比应有的工作要多得多。我也在实现 Font Awesome Icons,这看起来很简单,但是我发现的所有示例都将图标应用于标签,而没有任何标签页。经过一番折腾,我终于编译了这个,效果很好,并且不需要渲染器。

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:IBMobile.Views"
         xmlns:local2="clr-namespace:FontAwesome"
         x:Class="IBMobile.Views.HomePage"
         Visual="Material">
<ContentPage.IconImageSource>
    <FontImageSource FontFamily="{StaticResource FontAwesomeSolid}" Glyph="{x:Static local2:IconFont.Home}" />
</ContentPage.IconImageSource>...

回答设置FontSize的问题: 字体图标是一种字体,因此它的设置与其他任何字体一样。示例:

<Button x:Name="btnEmail" Clicked="BtnEmail_Clicked" CommandParameter="{Binding email}" FontFamily="{StaticResource FontAwesomeSolid}" FontSize="Large" Text="{x:Static local2:IconFont.Envelope}"  Grid.Column="3" Grid.Row="1" BackgroundColor="#007d5d" Margin="10" />

【讨论】:

  • 如何设置这样的图标大小?
  • 在 IconImageSource 中寻找 FA 的实现....谢谢....顺便说一句,在 中实现这个吗???
猜你喜欢
  • 2018-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-14
  • 2016-07-22
  • 2017-02-22
  • 2013-10-26
相关资源
最近更新 更多