Android Studio 预览在将选项卡项添加到选项卡布局时停止工作

问题描述 投票:0回答:1

我注意到预览在许多 PC 上崩溃,不仅仅是我的,但我遇到的最后一个问题是当我在 TabLayout 中添加 TabItem 时,我找不到任何解决方案。我试过以下方法:

  • 清理项目,重建项目
  • 使缓存无效并重新启动
  • 重启我的笔记本电脑
  • 检查该布局中的任何错误(没有,只有关于提取字符串的警告)
  • 查看材质组件版本(我目前最新的是1.8.0)

我可能忘记了一些愚蠢的事情,但现在我什么也想不起来。这是我的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.AdminActivity">


    <com.google.android.material.tabs.TabLayout
        android:id="@+id/adminTab"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:tabIndicatorColor="@color/befc_blue"
        app:tabSelectedTextColor="@color/befc_blue">


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

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@drawable/croissant"
            android:text="Hot Potatoes" />
    </com.google.android.material.tabs.TabLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

这些是我得到的错误:

我尝试将默认的 Material Design 样式添加到选项卡布局,但没有成功

android xml kotlin android-tablayout
1个回答
0
投票

我也遇到过这种情况,这似乎是建议降级到 1.17.0 的已知问题。 TabLayout 和 ViewHolder 的渲染问题

我确实找到了一个解决方法,它不是最好的解决方案,但我能够通过从 XML 中删除选项卡,然后以编程方式添加它们来解决预览中断问题。

 tabLayout.addTab(tabLayout.newTab().setText("Tab1"))
 tabLayout.addTab(tabLayout.newTab().setText("Tab2"))

它成功了所以我仍然可以看到我预览的其余部分,这让我畅通无阻。我还认为 Android Studio 专注于 Compose 布局修复与 XML。所以不确定这是否会得到修复。

© www.soinside.com 2019 - 2024. All rights reserved.