【问题标题】:Why floating action button is not displaying any src image and backgroud?为什么浮动操作按钮不显示任何 src 图像和背景?
【发布时间】:2021-05-17 07:08:18
【问题描述】:

我正在尝试将图像和背景添加到协调器布局中的浮动操作按钮,但我没有获得任何图像预览和背景颜色

fragment_task.xml

    <?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recycler_view_tasks"
        />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="16dp"
        app:srcCompat="@drawable/ic_add"
        app:tint="@color/teal_200"

        android:contentDescription="@string/todo" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

这是我的 build.gradle 也许这可以帮助确定问题

build.gradle

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-android-extensions'
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
    id 'androidx.navigation.safeargs.kotlin'
}

android {
    compileSdkVersion rootProject.compileSdkVersion
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.codinginflow.mvvmtodo"
        minSdkVersion rootProject.minSdkVersion
        targetSdkVersion rootProject.targetSdkVersion
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

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

    buildFeatures {
        viewBinding true
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
        freeCompilerArgs += "-Xopt-in=kotlinx.coroutines.ExperimentalCoroutinesApi"
    }
}

dependencies {
    // Default dependencies
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
    implementation "androidx.core:core-ktx:$ktxVersion"
    implementation "androidx.appcompat:appcompat:$appCompatVersion"
    implementation "com.google.android.material:material:$materialVersion"
    implementation "androidx.constraintlayout:constraintlayout:$constraintLayoutVersion"
    testImplementation "junit:junit:$junitVersion"
    androidTestImplementation "androidx.test.ext:junit:$testExtJunitVersion"
    androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion"

    // Fragment
    implementation "androidx.fragment:fragment-ktx:$fragmentVersion"

    // Lifecycle + ViewModel & LiveData
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycleVersion"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycleVersion"
    implementation "android.arch.lifecycle:common-java8:$lifecycleVersion"

    // Navigation Component
    implementation "androidx.navigation:navigation-fragment-ktx:$navigationVersion"
    implementation "androidx.navigation:navigation-ui-ktx:$navigationVersion"

    // Room
    implementation "androidx.room:room-runtime:$roomVersion"
    kapt "androidx.room:room-compiler:$roomVersion"
    implementation "androidx.room:room-ktx:$roomVersion"

    // Coroutines
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"

    // Dagger Hilt
    implementation "com.google.dagger:hilt-android:$hiltVersion"
    kapt "com.google.dagger:hilt-android-compiler:$hiltVersion"
    implementation "androidx.hilt:hilt-lifecycle-viewmodel:$hiltAndroidXVersion"
    kapt "androidx.hilt:hilt-compiler:$hiltAndroidXVersion"

    // DataStore
    implementation "androidx.datastore:datastore-preferences:$dataStoreVersion"

    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
}

kapt {
    correctErrorTypes true
}

浮动按钮在底部

告诉我你还需要哪些其他文件,我会编辑问题。

【问题讨论】:

  • 你能告诉materialVersion吗?
  • @Manu materialVersion = "1.3.0-alpha03"
  • 您能否尝试使用 1.3.0 并尝试实施 @Muhammad Ahmed 的回答,如果仍然无法正常工作,请告诉我。

标签: android xml kotlin


【解决方案1】:

尝试app:srcCompat 获取图像,app:backgroundTint 获取背景颜色

【讨论】:

    【解决方案2】:

    你试过app:icon吗,至少它可以和ExtendedFloatingActionButton一起使用

    【讨论】:

    • 也许这可以帮助你stackoverflow.com/questions/52069190/…>
    【解决方案3】:
        android:backgroundTint="@color/design_default_color_error"
        app:srcCompat="@drawable/ic_baseline_add_24"
        app:tint="@color/teal_200"
    

    您需要在父布局中添加此命名空间。

     xmlns:app="http://schemas.android.com/apk/res-auto"
    

    注意:如果您选择黑色背景并且不使用tint更改图标颜色,则默认为黑色,您将看不到图标

    【讨论】:

    • 能否请您提供渲染新图像的xml
    • 您上传了浮动图标显示白色方形视图的图像,因此您必须更改了 XML 中的某些内容,所以您也可以分享更新后的 XML
    • 当您在设备上运行时,我提供的解决方案可以正常工作吗?只是在 android studio 预览中失败了?
    • 是您的 Android 工作室,所有依赖项都是最新的,因为如果这是材料设计中的错误,它也应该发生在我身上,我的 Android 工作室和所有库都是最新的版本,根据我分享的解决方案,这些对我来说都很好
    • 是的,实现 'com.google.android.material:material:1.3.0'
    【解决方案4】:

    问题是因为一个错误而发生的,错误是Path.op is not supported

    所以解决方案是更新 Gradle 文件

    implementation 'com.google.android.material:material:1.2.0-alpha02'

    【讨论】:

    • alpha 版本最不稳定,然后是beta,它比alpha 更稳定,仍然存在很多潜在的错误和问题,最后是stable 版本。始终尝试使用稳定版本,直到您真正想要使用尚处于 alphabeta 阶段的某些功能。你试过implementation 'com.google.android.material:material:1.3.0' 吗?
    • 是的,你是对的,但它对我有用。
    • 还有这个implementation 'com.google.android.material:material:1.3.0' ?
    • 但除了材料设计版本之外,我的回答确实帮助您正确选择了正确的参数,例如 tint srcCompatbackgroundTint 对吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-15
    • 2019-04-05
    • 2020-07-17
    • 2017-03-27
    • 2017-02-04
    • 1970-01-01
    • 2015-07-26
    相关资源
    最近更新 更多