Android Studio预览在向标签布局添加标签项时停止工作。

huangapple go评论51阅读模式
英文:

Android Studio Preview Stops Working When Adding Tab Item to Tab Layout

问题

I've noticed the preview crashing on many PCs not just mine, but the last problem I'm experiencing is when I add a TabItem inside a TabLayout, and I can't find any solution. I've tried the following:

  • Clean Project, Rebuild Project
  • Invalidate caches and restart
  • Restarting my laptop
  • Checking for any errors in that layout (there is none, just warnings about extracting strings)
  • Checking the material components version (I have the current latest which is 1.8.0)

It might be something dumb that I forgot but right now I can't think of anything. Here's my XML code:

<?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>

These are the errors I get.

I've tried to add the default material design style to the tab layout but it didn't work.

英文:

I've noticed the preview crashing on many PCs not just mine, but the last problem I'm experiencing is when I add a TabItem inside a TabLayout, and I can't find any solution. I've tried the following:

  • Clean Project, Rebuild Project
  • Invalidate caches and restart
  • Restarting my laptop
  • Checking for any errors in that layout (there is none, just warnings about extracting strings)
  • Checking the material components version (I have the current latest which is 1.8.0)

It might be something dumb that I forgot but right now I can't think of anything. Here's my XML code:

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


    &lt;com.google.android.material.tabs.TabLayout
        android:id=&quot;@+id/adminTab&quot;
        android:layout_width=&quot;0dp&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginTop=&quot;8dp&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot;
        app:tabIndicatorColor=&quot;@color/befc_blue&quot;
        app:tabSelectedTextColor=&quot;@color/befc_blue&quot;&gt;


        &lt;com.google.android.material.tabs.TabItem
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:icon=&quot;@drawable/chart&quot;
            android:text=&quot;Reports&quot; /&gt;

        &lt;com.google.android.material.tabs.TabItem
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:icon=&quot;@drawable/croissant&quot;
            android:text=&quot;Hot Potatoes&quot; /&gt;
    &lt;/com.google.android.material.tabs.TabLayout&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

These are the errors I get:
Android Studio预览在向标签布局添加标签项时停止工作。

Android Studio预览在向标签布局添加标签项时停止工作。

Android Studio预览在向标签布局添加标签项时停止工作。

Android Studio预览在向标签布局添加标签项时停止工作。

I've tried to add the default material design style to the tab layout but it didn't work

答案1

得分: 1

我也遇到了这个问题,似乎这是一个已知问题,建议降级到1.17.0版本。链接

我找到了一个解决方法,虽然不是最佳解决方案,但通过从 XML 中移除选项卡,然后以编程方式添加它们,我成功地绕过了预览的问题。

tabLayout.addTab(tabLayout.newTab().setText("选项卡1"))
tabLayout.addTab(tabLayout.newTab().setText("选项卡2"))

这样我仍然能够看到我的预览的其余部分,这解除了我被阻止的困境。我还认为 Android Studio 更专注于 Compose 布局修复而不是 XML,所以不确定是否会修复这个问题。

英文:

I experienced this too, it seems like it's a known issue with the recommendation to downgrade to 1.17.0. https://stackoverflow.com/questions/75573385/render-problems-with-tablayout-and-viewholder

I did find a workaround, it's not the best solution but I was able to work around the preview breaking by removing the tabs from XML, then adding them in programatically.

 tabLayout.addTab(tabLayout.newTab().setText(&quot;Tab1&quot;))
 tabLayout.addTab(tabLayout.newTab().setText(&quot;Tab2&quot;))

It made it so I could still see the rest of my preview, which unblocked me. I also think Android Studio focuses on Compose layout fixes vs XML. So not sure if this will get fixed.

huangapple
  • 本文由 发表于 2023年4月6日 20:59:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949819.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定