【问题标题】:TabItem icon not showing with Material Components 1.1.0材质组件 1.1.0 中未显示 TabItem 图标
【发布时间】:2020-09-19 14:36:09
【问题描述】:

刚刚将我的应用程序升级到 Material Components 1.1.0,我的 TabLayout 中的图标不再显示。 我按照google material.io官网的教程,在Android Studio中预览不错,但是一旦启动应用,图标就没有出现了。

这是 XML 代码

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabLayout"
            android:layout_gravity="bottom"
            style="@style/Widget.MaterialComponents.TabLayout.Colored"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="center">

            <com.google.android.material.tabs.TabItem
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:icon="@drawable/ic_home"/>

            <com.google.android.material.tabs.TabItem
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:icon="@drawable/ic_fav"/>

            <com.google.android.material.tabs.TabItem
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:icon="@drawable/ic_about"/>


        </com.google.android.material.tabs.TabLayout>

    </androidx.viewpager.widget.ViewPager>

</RelativeLayout>

在我的 styles.xml 文件中,我定义了 colorPrimarycolorOnPrimary 用于为图标和 TabLayout 背景颜色着色。

但是,即使Android Studio中的预览效果很好,一旦启动应用程序,我的TabLayout的背景以正确的颜色显示,但图标不显示。

编辑:

只需使用调试模式,每个选项卡项的选项卡图标为空:

编辑 2:

我找到了问题,但没有找到解决方案! 问题出在这一行:

tabLayout.setupWithViewPager(viewPager)

执行此行后,TabLayout 中的所有图标都重置为 null。 通过禁用这条线,我拥有了所有的图标。 但是,如何在 TabLayout 上设置带有图标的 ViewPager ? 如果我手动设置它们,我将不会拥有从 MDC 继承的颜色

【问题讨论】:

  • 也上传你的styles.xml文件
  • 我已经做了,它继承自Theme.MaterialComponents.Light.NoActionBar
  • colorPrimary 和 colorOnPrimary 的值是什么?
  • &lt;color name="colorPrimary"&gt;#00838f&lt;/color&gt; & &lt;color name="colorOnPrimary"&gt;#ff8f00&lt;/color&gt; 我不知道这对你有什么帮助
  • 代码对我来说似乎很好......我试过它们显示正确......

标签: android kotlin material-design android-tablayout material-components-android


【解决方案1】:

tabLayout.setupWithViewPager() 方法删除所有选项卡,然后从适应的选项卡中添加选项卡。
要添加图标,您可以使用以下内容:

for (int i = 0; i < tabLayout.getTabCount(); i++) {
        tabLayout.getTabAt(i).setIcon(iconResId);
}

如果我手动设置它们,我不会有从 MDC 继承的颜色

com.google.android.material.tabs.TabLayout默认使用Widget.MaterialComponents.TabLayout 样式。如果您想覆盖图标色调,只需在布局或自定义样式中使用 tabIconTint

<com.google.android.material.tabs.TabLayout
  app:tabIconTint="@color/my_selector"
  ...>

【讨论】:

  • 这样做时,是否会考虑到图标上 TabLayout 的样式及其色调?
  • @mamenejr viewPager 不会改变 tabLayout 样式。
  • @mamenejr google 关于样式或 viewPager 的正常行为?
  • ViewPager 禁用 TabsLayout 上的图标
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-06
  • 2020-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多