Android 当布局中有浮动操作按钮时,Studio 预览不显示
Android Studio preview doesn't show when a floating action button is in the layout
正如标题所暗示的那样,当我在任何 xml 文件的布局中的任何位置都有一个浮动操作按钮时,预览将停止工作,我可以从字面上注释掉它,它会立即完美地重新绘制。有什么想法吗?
我试过多次重置同步、使缓存无效等。
当我重置视图时,视图呈现良好,但仅对于打开的页面,我在之后打开的任何页面都不会呈现,导航回先前呈现的布局会破坏它
我唯一能想到的是我在我的项目中使用了 clans 浮动操作按钮,但是 Google 破坏了我的预览。
编辑 我已经删除了 clans 清理项目无效缓存重新启动但问题仍然存在
另外,如果我将 FloatingActionButton 更改为 ExtendedFloatingActionButton,预览效果很好,查看这两个似乎它们的扩展完全不同 类
也在使用 AndroidX
这是一个 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=".settings.PdfSettingsActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:scaleType="centerCrop"
android:layout_margin="@dimen/activity_vertical_margin_16dp"
android:elevation="@dimen/activity_horizontal_margin_4dp"
android:id="@+id/pdf_renderer"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/share"
android:layout_margin="@dimen/activity_vertical_margin_16dp"
android:src="@drawable/ic_share_white_24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
如果我注释掉浮动操作按钮,视图会立即呈现
com.google.android.material.floatingactionbutton.FloatingActionButton
Android Studio 3.4.1 Build #AI-183.6156.11.34.5522156, built on May 1,
2019 JRE: 1.8.0_152-release-1343-b01 x86_64 JVM: OpenJDK 64-Bit Server
VM by JetBrains s.r.o macOS 10.14.3
编辑
现在只是玩弄它,我设法让它抛出一个错误:
java.lang.IllegalArgumentException: java.lang.ClassCastException@54291cbb
at sun.reflect.GeneratedMethodAccessor971.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at android.animation.PropertyValuesHolder_Delegate.callMethod(PropertyValuesHolder_Delegate.java:108)
at android.animation.PropertyValuesHolder_Delegate.nCallFloatMethod(PropertyValuesHolder_Delegate.java:143)
at android.animation.PropertyValuesHolder.nCallFloatMethod(PropertyValuesHolder.java)
at android.animation.PropertyValuesHolder.access0(PropertyValuesHolder.java:38)
at android.animation.PropertyValuesHolder$FloatPropertyValuesHolder.setAnimatedValue(PropertyValuesHolder.java:1387)
at android.animation.ObjectAnimator.animateValue(ObjectAnimator.java:990)
at android.animation.ValueAnimator.animateBasedOnTime(ValueAnimator.java:1339)
at android.animation.ValueAnimator.doAnimationFrame(ValueAnimator.java:1471)
at android.animation.ValueAnimator.pulseAnimationFrame(ValueAnimator.java:1490)
at android.animation.AnimatorSet.pulseFrame(AnimatorSet.java:1163)
at android.animation.AnimatorSet.handleAnimationEvents(AnimatorSet.java:1146)
at android.animation.AnimatorSet.doAnimationFrame(AnimatorSet.java:1046)
at android.animation.AnimationHandler.doAnimationFrame(AnimationHandler.java:146)
at android.animation.AnimationHandler.access0(AnimationHandler.java:37)
at android.animation.AnimationHandler.doFrame(AnimationHandler.java:54)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:947)
at android.view.Choreographer.doCallbacks(Choreographer.java:761)
at android.view.Choreographer_Delegate.doFrame(Choreographer_Delegate.java:66)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.renderAndBuildResult(RenderSessionImpl.java:563)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.render(RenderSessionImpl.java:425)
at com.android.layoutlib.bridge.BridgeRenderSession.render(BridgeRenderSession.java:120)
at com.android.ide.common.rendering.api.RenderSession.render(RenderSession.java:151)
at com.android.ide.common.rendering.api.RenderSession.render(RenderSession.java:133)
at com.android.tools.idea.rendering.RenderTask.lambda$null(RenderTask.java:755)
at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
build.gradle 依赖关系
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
//Room dependencies
implementation 'androidx.room:room-runtime:2.1.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.firebase:firebase-core:16.0.4'
implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
implementation 'com.google.firebase:firebase-database:16.0.4'
implementation 'com.google.firebase:firebase-storage:16.0.4'
implementation 'com.google.firebase:firebase-auth:16.0.5'
implementation 'com.firebase:firebase-client-android:2.4.0'
annotationProcessor 'androidx.room:room-compiler:2.1.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0-alpha01'
annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.2.0-alpha01"
//GSON
implementation 'com.google.code.gson:gson:2.8.5'
//UI design
implementation 'com.github.hotchemi:android-rate:1.0.1'
implementation "androidx.legacy:legacy-support-core-utils:1.0.0"
implementation 'com.google.android.material:material:1.1.0-alpha07'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0-alpha06'
implementation 'com.github.siyamed:android-shape-imageview:0.9.+@aar'
implementation 'com.getkeepsafe.taptargetview:taptargetview:1.12.0'
implementation 'com.romandanylyk:pageindicatorview:1.0.3'
implementation 'com.github.VRGsoftUA:ParallaxView:1.0'
//Glide
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
//Edit image
implementation 'com.yashoid:instacropper:1.0.6'
// FloatingActionButton
implementation 'com.github.martipello:FloatingActionButton:v2.0'
//Navigation
implementation "androidx.navigation:navigation-fragment:2.1.0-alpha05"
// For Kotlin use navigation-fragment-ktx
implementation "androidx.navigation:navigation-ui:2.1.0-alpha05"
//material components
implementation 'androidx.appcompat:appcompat:1.1.0-beta01'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha4'
implementation 'androidx.vectordrawable:vectordrawable:1.1.0-beta02'
//tests
testImplementation 'org.json:json:20180130'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.2.0'
implementation 'androidx.test.espresso:espresso-idling-resource:3.2.0'
androidTestImplementation 'androidx.test.espresso.idling:idling-concurrent:3.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.2.0'
implementation 'org.apache.commons:commons-lang3:3.8.1'
}
我猜你在Activity或者Fragment中的cast写错了
检查您是否正确导入了 FloatingActionButton
class。它与您在 xml.
中的视图相对应
例如,
FloatingButton btnFab = (FloatingButton) findViewById(R.id.btn_fab) // Not FloatingActionButton
在你的 xml.
<!-- Not FloatingButton -->
<com.example.FloatingActionButton
android:id="@+id/btn_fab"
android:layout_width = "45dp"
android:layout_height = "45dp"
/>
检查 class 个名称是否匹配。
我注意到您使用的是 AndroidX。由于您不能将支持库与 AndroidX 一起使用,因此请删除 build.gradle
文件中的所有支持库并添加:
implementation 'com.google.android.material:material:1.1.0-alpha07'
到您的 app/build.gradle
文件。
编辑:
如果这不起作用,请检查
android.enableJetifier=true
android.useAndroidX=true
在您的 gradle.properties
文件中。
你试过弄乱按钮的高度吗?
android:elevation
或
app:elevation
似乎是 Android Studio 的问题,应在 3.5 beta 2 (as well as the latest beta I imagine). More specifically, this seems to have been Issue#129926965 中修复。
来源:here
这是 bug/issue 在 Android Studio 3 中的已知功能。4.x https://issuetracker.google.com/issues/132316448 您只需等待 Android Studio 3.5。但是,如果您还使用 Beta 版本,那么这个问题似乎在 Beta 2 及更高版本中得到了纠正,但尚未得到证实。
与 documentation... 比较时,您可能必须使用属性
srcCompat
改为:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
app:srcCompat="@drawable/ic_share_white_24dp"/>
虽然 build.gradle
的这一部分显得混乱/多余:
implementation "com.github.martipello:FloatingActionButton:v2.0"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
尝试在代码部分而不是设计部分手动编写这些行。
它对我有用
正如标题所暗示的那样,当我在任何 xml 文件的布局中的任何位置都有一个浮动操作按钮时,预览将停止工作,我可以从字面上注释掉它,它会立即完美地重新绘制。有什么想法吗?
我试过多次重置同步、使缓存无效等。 当我重置视图时,视图呈现良好,但仅对于打开的页面,我在之后打开的任何页面都不会呈现,导航回先前呈现的布局会破坏它
我唯一能想到的是我在我的项目中使用了 clans 浮动操作按钮,但是 Google 破坏了我的预览。
编辑 我已经删除了 clans 清理项目无效缓存重新启动但问题仍然存在
另外,如果我将 FloatingActionButton 更改为 ExtendedFloatingActionButton,预览效果很好,查看这两个似乎它们的扩展完全不同 类
也在使用 AndroidX
这是一个 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=".settings.PdfSettingsActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:scaleType="centerCrop"
android:layout_margin="@dimen/activity_vertical_margin_16dp"
android:elevation="@dimen/activity_horizontal_margin_4dp"
android:id="@+id/pdf_renderer"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/share"
android:layout_margin="@dimen/activity_vertical_margin_16dp"
android:src="@drawable/ic_share_white_24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
如果我注释掉浮动操作按钮,视图会立即呈现
com.google.android.material.floatingactionbutton.FloatingActionButton
Android Studio 3.4.1 Build #AI-183.6156.11.34.5522156, built on May 1, 2019 JRE: 1.8.0_152-release-1343-b01 x86_64 JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o macOS 10.14.3
编辑
现在只是玩弄它,我设法让它抛出一个错误:
java.lang.IllegalArgumentException: java.lang.ClassCastException@54291cbb
at sun.reflect.GeneratedMethodAccessor971.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at android.animation.PropertyValuesHolder_Delegate.callMethod(PropertyValuesHolder_Delegate.java:108)
at android.animation.PropertyValuesHolder_Delegate.nCallFloatMethod(PropertyValuesHolder_Delegate.java:143)
at android.animation.PropertyValuesHolder.nCallFloatMethod(PropertyValuesHolder.java)
at android.animation.PropertyValuesHolder.access0(PropertyValuesHolder.java:38)
at android.animation.PropertyValuesHolder$FloatPropertyValuesHolder.setAnimatedValue(PropertyValuesHolder.java:1387)
at android.animation.ObjectAnimator.animateValue(ObjectAnimator.java:990)
at android.animation.ValueAnimator.animateBasedOnTime(ValueAnimator.java:1339)
at android.animation.ValueAnimator.doAnimationFrame(ValueAnimator.java:1471)
at android.animation.ValueAnimator.pulseAnimationFrame(ValueAnimator.java:1490)
at android.animation.AnimatorSet.pulseFrame(AnimatorSet.java:1163)
at android.animation.AnimatorSet.handleAnimationEvents(AnimatorSet.java:1146)
at android.animation.AnimatorSet.doAnimationFrame(AnimatorSet.java:1046)
at android.animation.AnimationHandler.doAnimationFrame(AnimationHandler.java:146)
at android.animation.AnimationHandler.access0(AnimationHandler.java:37)
at android.animation.AnimationHandler.doFrame(AnimationHandler.java:54)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:947)
at android.view.Choreographer.doCallbacks(Choreographer.java:761)
at android.view.Choreographer_Delegate.doFrame(Choreographer_Delegate.java:66)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.renderAndBuildResult(RenderSessionImpl.java:563)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.render(RenderSessionImpl.java:425)
at com.android.layoutlib.bridge.BridgeRenderSession.render(BridgeRenderSession.java:120)
at com.android.ide.common.rendering.api.RenderSession.render(RenderSession.java:151)
at com.android.ide.common.rendering.api.RenderSession.render(RenderSession.java:133)
at com.android.tools.idea.rendering.RenderTask.lambda$null(RenderTask.java:755)
at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
build.gradle 依赖关系
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
//Room dependencies
implementation 'androidx.room:room-runtime:2.1.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.firebase:firebase-core:16.0.4'
implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
implementation 'com.google.firebase:firebase-database:16.0.4'
implementation 'com.google.firebase:firebase-storage:16.0.4'
implementation 'com.google.firebase:firebase-auth:16.0.5'
implementation 'com.firebase:firebase-client-android:2.4.0'
annotationProcessor 'androidx.room:room-compiler:2.1.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0-alpha01'
annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.2.0-alpha01"
//GSON
implementation 'com.google.code.gson:gson:2.8.5'
//UI design
implementation 'com.github.hotchemi:android-rate:1.0.1'
implementation "androidx.legacy:legacy-support-core-utils:1.0.0"
implementation 'com.google.android.material:material:1.1.0-alpha07'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0-alpha06'
implementation 'com.github.siyamed:android-shape-imageview:0.9.+@aar'
implementation 'com.getkeepsafe.taptargetview:taptargetview:1.12.0'
implementation 'com.romandanylyk:pageindicatorview:1.0.3'
implementation 'com.github.VRGsoftUA:ParallaxView:1.0'
//Glide
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
//Edit image
implementation 'com.yashoid:instacropper:1.0.6'
// FloatingActionButton
implementation 'com.github.martipello:FloatingActionButton:v2.0'
//Navigation
implementation "androidx.navigation:navigation-fragment:2.1.0-alpha05"
// For Kotlin use navigation-fragment-ktx
implementation "androidx.navigation:navigation-ui:2.1.0-alpha05"
//material components
implementation 'androidx.appcompat:appcompat:1.1.0-beta01'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha4'
implementation 'androidx.vectordrawable:vectordrawable:1.1.0-beta02'
//tests
testImplementation 'org.json:json:20180130'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.2.0'
implementation 'androidx.test.espresso:espresso-idling-resource:3.2.0'
androidTestImplementation 'androidx.test.espresso.idling:idling-concurrent:3.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.2.0'
implementation 'org.apache.commons:commons-lang3:3.8.1'
}
我猜你在Activity或者Fragment中的cast写错了
检查您是否正确导入了 FloatingActionButton
class。它与您在 xml.
例如,
FloatingButton btnFab = (FloatingButton) findViewById(R.id.btn_fab) // Not FloatingActionButton
在你的 xml.
<!-- Not FloatingButton -->
<com.example.FloatingActionButton
android:id="@+id/btn_fab"
android:layout_width = "45dp"
android:layout_height = "45dp"
/>
检查 class 个名称是否匹配。
我注意到您使用的是 AndroidX。由于您不能将支持库与 AndroidX 一起使用,因此请删除 build.gradle
文件中的所有支持库并添加:
implementation 'com.google.android.material:material:1.1.0-alpha07'
到您的 app/build.gradle
文件。
编辑:
如果这不起作用,请检查
android.enableJetifier=true
android.useAndroidX=true
在您的 gradle.properties
文件中。
你试过弄乱按钮的高度吗?
android:elevation
或
app:elevation
似乎是 Android Studio 的问题,应在 3.5 beta 2 (as well as the latest beta I imagine). More specifically, this seems to have been Issue#129926965 中修复。
来源:here
这是 bug/issue 在 Android Studio 3 中的已知功能。4.x https://issuetracker.google.com/issues/132316448 您只需等待 Android Studio 3.5。但是,如果您还使用 Beta 版本,那么这个问题似乎在 Beta 2 及更高版本中得到了纠正,但尚未得到证实。
与 documentation... 比较时,您可能必须使用属性
srcCompat
改为:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
app:srcCompat="@drawable/ic_share_white_24dp"/>
虽然 build.gradle
的这一部分显得混乱/多余:
implementation "com.github.martipello:FloatingActionButton:v2.0"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
尝试在代码部分而不是设计部分手动编写这些行。 它对我有用