deo*_*ens 5 android android-layout android-studio android-jetpack android-jetpack-compose
我正在尝试在 XML 布局文件中使用 ComposeView 并且应用程序工作正常,但我在预览中遇到了渲染问题。如果没有办法解决这个问题,我可以在预览渲染时以某种方式(例如通过工具标签)忽略这个 ComposeView 吗?主要目的 - 我想查看其他 XML 元素,不需要 ComposeView(当然仅用于预览)。谢谢
Android Studio Preview 窗口显示以下问题:
java.lang.IllegalStateException: ViewTreeLifecycleOwner is not present in this window. Use
ComponentActivity, FragmentActivity or AppCompatActivity to configure ViewTreeLifecycleOwner
automatically, or call ViewTreeLifecycleOwner.set() for this View or an ancestor in the same
window.
at androidx.compose.ui.platform.AbstractComposeView.onAttachedToWindow(ComposeView.kt:176)
at android.view.View.dispatchAttachedToWindow(View.java:20479)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3489)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
at android.view.AttachInfo_Accessor.setAttachInfo(AttachInfo_Accessor.java:44)
Run Code Online (Sandbox Code Playgroud)
简化的xml文件,仅举个例子
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".presentation.home.HomeActivity">
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I want to see at least this text on preview"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/hello_view"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<androidx.compose.ui.platform.ComposeView
android:id="@+id/hello_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/text_view"
tools:context=".presentation.home.HomeActivity">
</androidx.compose.ui.platform.ComposeView>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1352 次 |
| 最近记录: |