首页 > 解决方案 > AndroidX 中的 Xamarin Forms 5.0 TabbedPage 选项卡标题文本自定义

问题描述

我正在开发 Xamarin Forms 应用程序。要求是将标签页的标题设置为字母大小写 + 使用自定义字体。

正如您在本文中看到的那样我发现了一些解决方案,其中需要在文件 Resources/layout/Tabbar.xml 中更改样式以实现所需的结果。

但是,不幸的是 Xamarin Forms 5.0 使用 AndroidX 并从默认创建中删除了资源/布局目录。并且还从 Android 项目的 MainActivity.cs 中删除了这两行代码:

        TabLayoutResource = Resource.Layout.Tabbar; // Removed in AndroidX and Xamarin Forms Ver 5.0
        ToolbarResource = Resource.Layout.Toolbar;  // Removed in AndroidX and Xamarin Forms Ver 5.0

正如您在 Xamarin 官方文档中看到的那样

现在我很好奇如何使用 AndroidX 在更新的 XAMARIN FORMS 版本 5.0 中对 TabbedPage 标题文本进行自定义样式?任何想法?

标签: xamarinxamarin.formsxamarin.androidandroidxtabbedpage

解决方案


您应该将 保留 TabLayoutResource = Resource.Layout.Tabbar;在您的 MainActivity 中。

Tabbar.xml类的:

<com.google.android.material.tabs.TabLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:id="@+id/sliding_tabs"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="?android:attr/colorPrimary"
   android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
   ...
   app:tabTextAppearance="@style/MyCustomTextAppearance"  //make your title to Letter-Case 

/>

然后定义MyCustomTextAppearance你的风格Resources/styles

<style name="MyCustomTextAppearance" parent="TextAppearance.Design.Tab">
     <item name="textAllCaps">false</item>
</style>

 

推荐阅读