首页 > 解决方案 > 浮动操作按钮 Android Studio 在预览中不显示背景颜色和图像

问题描述

我是安卓新手。我正在尝试学习浮动操作按钮。我在youtube上关注这个视频。

但是我的预览没有显示 + 图标,也没有显示我在代码中应用的背景颜色,如您所见。当我在实际设备上运行我的项目时,它会同时显示图像和背景颜色。但不在我的 android 预览中。

activity_main.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=".MainActivity">

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="40dp"
        android:layout_marginBottom="36dp"
        android:backgroundTint="@color/design_default_color_primary"
        android:clickable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@drawable/ic_add_black_24dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

预习

我在 android Studio 中的预览截图

构建.gradle(应用程序)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

    implementation 'com.google.android.material:material:1.2.1'
}

我的配置

  1. 安卓工作室:3.6.1

安卓工作室 3.6.1

Build #AI-192.7142.36.36.6241897, built on February 27, 2020
Runtime version: 1.8.0_212-release-1586-b04 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0
GC: ParNew, ConcurrentMarkSweep
Memory: 1237M
Cores: 8
Registry: ide.new.welcome.screen.force=true
Non-Bundled Plugins: 

我不确定,但可能与此有关

标签: androidfloating-action-button

解决方案


这可能是您正在使用的 Android Studio 版本中的一个错误。

您可以尝试最新的更新版本来解决此错误。

更新 Android Studio

Android Studio > Menu bar > Help>check for updates.

推荐阅读