为什么我不能在 Xamarin.Android 中的 Material TabLayout 的选项卡中更改文本颜色?

Why cannot I change the text color in the tabs of my Material TabLayout in Xamarin.Android?

我在我的应用程序中集成了一些选项卡,但即使我在其颜色中设置了一些灰色阴影,这些选项卡仍保持文本颜色为白色:

tabs.SetTabTextColors(Color.ParseColor("#bdbdbd"), Color.ParseColor("#212121"));

这是它的样子:

这是我的 XML:


<?xml version="1.0" encoding="UTF-8" ?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">
    <LinearLayout
        android:theme="@style/AppTheme"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <com.jsibbold.zoomage.ZoomageView
            android:adjustViewBounds="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/imgRuin"
            app:zoomage_restrictBounds="false"
            app:zoomage_animateOnReset="true"
            app:zoomage_autoResetMode="UNDER"
            app:zoomage_autoCenter="true"
            app:zoomage_zoomable="true"
            app:zoomage_translatable="true"
            app:zoomage_minScale="0.6"
            app:zoomage_maxScale="8" />
        <TextView
            android:layout_marginTop="5dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:textStyle="italic"
            android:textSize="12dp"
            android:textColor="#4c8c4a"
            android:id="@+id/lblImgDescription" />
        <com.google.android.material.tabs.TabLayout
            android:id="@+id/mTabOptions"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.google.android.material.tabs.TabItem
                android:id="@+id/tabOverview"
                android:text="@string/tabOverview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <com.google.android.material.tabs.TabItem
                android:id="@+id/tabPreview"
                android:text="@string/tabPreview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <com.google.android.material.tabs.TabItem
                android:id="@+id/tabDetails"
                android:text="@string/tabDetails"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <com.google.android.material.tabs.TabItem
                android:id="@+id/tabLocation"
                android:text="@string/tabLocation"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </com.google.android.material.tabs.TabLayout>
        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
</ScrollView>

这是我的风格:


<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <style name="AppTheme.Launcher" parent="android:Theme.Material.Light">
        <item name="android:windowBackground">@drawable/launch_screen</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>
    </style>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    <!-- ToolBar -->
    <style name="ToolBarStyle" parent="Theme.AppCompat">
        <item name="android:textColorPrimary">@android:color/white</item>
        <item name="android:textColorSecondary">@android:color/white</item>
        <item name="actionMenuTextColor">@android:color/white</item>
        <item name="android:itemTextAppearance">@android:color/white</item>
    </style>
    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
    <style name="progressBarStyle" parent="@style/Widget.AppCompat.ProgressBar.Horizontal">
        <item name="android:indeterminateDrawable">@drawable/progress_bar_drawable</item>
    </style>
</resources>

这些是颜色:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#1b5e20</color>
    <color name="colorPrimaryDark">#003300</color>
    <color name="colorAccent">#4c8c4a</color>
</resources>

知道为什么它们总是白色的吗?

尝试将这两个属性都放入您的 TabLayout:

app:tabSelectedTextColor="#f01654"
app:tabTextColor="#1652f0"

您的最终代码如下所示:

<com.google.android.material.tabs.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:layout_gravity="bottom"
    android:background="@color/button_background"
    android:fillViewport="true"
    app:tabBackground="@drawable/fixed_bottom_button"
    app:tabIndicatorColor="@color/color_primary_text"
    app:tabMode="fixed"
    app:tabSelectedTextColor="#f01654"
    app:tabTextColor="#1652f0" />

似乎没有打印文本:

public class TabConfigurationStrategy : Java.Lang.Object, TabLayoutMediator.ITabConfigurationStrategy
{
    public void OnConfigureTab(TabLayout.Tab p0, int p1)
    {
        switch (p1)
        {
            case 0:
                p0.SetText(Resource.String.tabOverview);
                break;
            case 1:
                p0.SetText(Resource.String.tabPreview);
                break;
            case 2:
                p0.SetText(Resource.String.tabDetails);
                break;
            case 3:
                p0.SetText(Resource.String.tabLocation);
                break;
        }
    }
}

这是我必须添加的内容。