【问题标题】:Specific ContentPage Title Font size on androidandroid上的特定ContentPage Title字体大小
【发布时间】:2018-04-15 16:39:04
【问题描述】:

所以我创建了一个导航栏,我想在其中为 Android 用户缩小标题字体。

我想我可以这样做:

<ContentPage.Title>
    <OnPlatform x:TypeArguments="Size">
        <On Platform="Android">8</On>
    </OnPlatform>
</ContentPage.Title>

但它不起作用,我得到一个编译器错误。

因此,我该如何更正我的代码,或者最好在 App.xaml 中编写一些可重用的代码,以便为我的导航标题声明一个特定于平台的字体大小。

【问题讨论】:

标签: android xaml xamarin xamarin.forms


【解决方案1】:

您需要使用自定义渲染器来设置字体大小。
例如:
在 pcl 项目中创建一个MyNavigationPage.cs:

public class MyNavigationPage : NavigationPage
{
    public MyNavigationPage(Page root) : base(root)
    {

    }
}

并在android项目中创建渲染器类:

[assembly: ExportRenderer(typeof(MyNavigationPage), typeof(CustomNavigationPageRenderer))]    
namespace NavigationBarFontSize.Droid
{
    public class CustomNavigationPageRenderer : NavigationPageRenderer
    {
        public CustomNavigationPageRenderer(Context context) : base(context)
        {

        }

        private Android.Support.V7.Widget.Toolbar toolbar;

        public override void OnViewAdded(Android.Views.View child)
        {
            base.OnViewAdded(child);
            if (child.GetType() == typeof(Android.Support.V7.Widget.Toolbar))
            {
                toolbar = child as Android.Support.V7.Widget.Toolbar;
                toolbar.ChildViewAdded += Toolbar_ChildViewAdded;
                var a = toolbar.ChildCount;
            }
        }

        void Toolbar_ChildViewAdded(object sender, ChildViewAddedEventArgs e)
        {
            var view = e.Child.GetType();
            if (e.Child.GetType() == typeof(Android.Support.V7.Widget.AppCompatTextView))
            {
                var textView = e.Child as Android.Support.V7.Widget.AppCompatTextView;
                textView.TextSize = 50;
                toolbar.ChildViewAdded -= Toolbar_ChildViewAdded;
            }
        }
    }
}

最后,您可以通过以下方式使用它:

        public App ()
        {
            InitializeComponent();    
            MainPage = new MyNavigationPage(new MainPage());
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 2014-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-26
    • 2014-02-15
    • 2012-07-02
    相关资源
    最近更新 更多