【发布时间】: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 文件中,我定义了 colorPrimary 和 colorOnPrimary 用于为图标和 TabLayout 背景颜色着色。
但是,即使Android Studio中的预览效果很好,一旦启动应用程序,我的TabLayout的背景以正确的颜色显示,但图标不显示。
编辑:
只需使用调试模式,每个选项卡项的选项卡图标为空:
编辑 2:
我找到了问题,但没有找到解决方案! 问题出在这一行:
tabLayout.setupWithViewPager(viewPager)
执行此行后,TabLayout 中的所有图标都重置为 null。 通过禁用这条线,我拥有了所有的图标。 但是,如何在 TabLayout 上设置带有图标的 ViewPager ? 如果我手动设置它们,我将不会拥有从 MDC 继承的颜色
【问题讨论】:
-
也上传你的styles.xml文件
-
我已经做了,它继承自
Theme.MaterialComponents.Light.NoActionBar -
colorPrimary 和 colorOnPrimary 的值是什么?
-
<color name="colorPrimary">#00838f</color>&<color name="colorOnPrimary">#ff8f00</color>我不知道这对你有什么帮助 -
代码对我来说似乎很好......我试过它们显示正确......
标签: android kotlin material-design android-tablayout material-components-android