【问题标题】:Why cannot I change the text color in the tabs of my Material TabLayout in Xamarin.Android?为什么我无法更改 Xamarin.Android 中 Material TabLayout 选项卡中的文本颜色?
【发布时间】:2021-09-17 08:21:02
【问题描述】:

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

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>

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

【问题讨论】:

    标签: android xamarin.android android-tablayout material-components-android


    【解决方案1】:

    文字好像没有打印出来:

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

    这是我必须添加的。

    【讨论】:

      【解决方案2】:

      尝试将这两个属性都放入您的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" />
      

      【讨论】:

      • 它不会改变任何颜色。现在,它是透明的。另外,我正在使用这个:com.google.android.material.tabs.TabLayout 而不是这个android.support.design.widget.TabLayout
      • 我更新了我的答案。这对我来说很好。
      • @FedericoNavarrete 它也适用于我。尝试清理您的项目并重建它。
      • 完美。如果您能将其标记为正确答案,将不胜感激。祝你的项目好运!干杯
      • 感谢您的尝试,但这不是我的情况。当您使用 ViewPager2 和 TabConfigurationStrategy 时,似乎有不同的逻辑。我写了自己的答案。
      猜你喜欢
      • 1970-01-01
      • 2018-03-26
      • 1970-01-01
      • 1970-01-01
      • 2017-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-28
      相关资源
      最近更新 更多