#android #xamarin.android #android-tablayout #material-components-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>
Есть идеи, почему они всегда белые?
Ответ №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" />
Комментарии:
1. Он не меняет никакого цвета. Теперь это становится прозрачным. Кроме того, я использую вот это:
com.google.android.material.tabs.TabLayout
не этоandroid.support.design.widget.TabLayout
2. Я обновил свой ответ. Для меня это прекрасно работает.
3. @FedericoNavarrete Это работает и для меня. Попробуйте очистить свой проект и перестроить его.
4. Идеальный. Буду признателен, если вы сможете отметить это как правильный ответ. Удачи вам в вашем проекте! Овации
5. Спасибо за вашу попытку, но это был не мой случай. Похоже, что при использовании ViewPager2 и TabConfigurationStrategy существует иная логика. Я написал свой собственный ответ по этому поводу.