简体   繁体   English

如何更改 Xamarin.Forms 中 TabbedPage 上标记所选选项卡的“下划线”的颜色?

[英]How to do you change the color of the "underline" marking the selected tab on a TabbedPage in Xamarin.Forms?

I'm currently adding several different themes (10-ish) to a Xamarin.Forms app.我目前正在向 Xamarin.Forms 应用程序添加几个不同的主题(10-ish)。 I've figured out how to dynamically change the color of everything except the "underline" on a tab on TabbedPage (see the attached image).我已经想出了如何动态更改除 TabbedPage 选项卡上的“下划线”以外的所有内容的颜色(请参见附图)。

Screenshot identifying the TabbedPage "underline" with a red rectangle用红色矩形标识 TabbedPage“下划线”的屏幕截图

I've tried to set the five different color properties (that I know of) on the TabbedPage as well as the BackgroundColor on the ContentPages in the tabs:我尝试在 TabbedPage 上设置五种不同的颜色属性(我知道)以及选项卡中 ContentPages 上的 BackgroundColor:

TabbedPage tabbedPage = new TabbedPage { Title = "Tabbed Page" };

tabbedPage.BarBackgroundColor = Color.Black;
tabbedPage.BarTextColor = Color.Gray;
tabbedPage.SelectedTabColor = Color.Black;
tabbedPage.UnselectedTabColor = Color.Black;

tabbedPage.BackgroundColor = Color.Black;

tabbedPage.Children.Add(new ContentPage { Title = "Page 1", BackgroundColor = Color.Gray });
tabbedPage.Children.Add(new ContentPage { Title = "Page 2", BackgroundColor = Color.Gray });

await Application.Current.MainPage.Navigation.PushAsync(tabbedPage);

Is it possible to set the color of the "underline" in Xamarin.Forms?是否可以在 Xamarin.Forms 中设置“下划线”的颜色? Or do I need to make platform-specific changes to achieve this?或者我是否需要进行特定于平台的更改才能实现这一目标?

Use app:tabIndicatorColor.使用 app:tabIndicatorColor。

All you have to do is to create a Tabar.xml file with below code in the layout folder(create one if needed) in the Android project's Resources folder and change the color via app:tabIndicatorColor;您所要做的就是在 Android 项目的 Resources 文件夹中的 layout 文件夹中使用以下代码创建一个 Tabar.xml 文件(如果需要,创建一个),然后通过 app:tabIndicatorColor; 更改颜色;

<?xml version="1.0" encoding="utf-8"?>
<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/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="#ff0000"/>

And then add below code in onCreate method of MainActivity.cs然后在 MainActivity.cs 的 onCreate 方法中添加以下代码

TabLayoutResource = Resource.Layout.Tabar;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM