【问题标题】:Floating Action Button appears as a gray box浮动操作按钮显示为灰色框
【发布时间】:2019-04-26 07:59:41
【问题描述】:

我试图在屏幕右下角有一个 FAB 以浮动在 recyclerview 的顶部。然而,FAB 只是一个带有小部件路径的灰色框。

问题来了:

这是我的 XML 实现:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

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

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/generate_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_margin="16dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:clickable="true"
        android:focusable="true" />

</RelativeLayout>

依赖:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'com.android.support:design:28.0.1'
    implementation 'com.google.android.material:material:1.1.0-alpha05'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

【问题讨论】:

  • 你添加设计依赖了吗?
  • 是的。检查编辑。
  • 您是否在其中插入了任何可绘制对象?我没有看到任何与之相关的可绘制对象。

标签: android android-layout android-recyclerview floating-action-button


【解决方案1】:

试试file&gt;invalidate caches/restart。这就是我的一些小部件变灰时遇到的问题。

【讨论】:

    【解决方案2】:

    抱歉我没看到图片

    我用这个库

    implementation 'com.google.android.material:material:1.0.0'
    

    版本号为 1.0.0

    您可以尝试清理您的项目缓存并重建它。

    【讨论】:

    • 为什么需要为FAB添加src?
    【解决方案3】:

    您添加了错误的 FloatingActionButton。 实现这个并添加一个 src 属性。

    <android.support.design.widget.FloatingActionButton
     android:id="@+id/generate_fab"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentBottom="true"
     android:layout_alignParentEnd="true"
     android:layout_alignParentRight="true"
     android:layout_marginBottom="16dp"
     android:layout_marginRight="16dp"
     android:clickable="true"
     android:focusable="true" 
     android:src="@drawable/ic_add_black_24dp"/>
    

    【讨论】:

    • 不是因为invalidate caches/restart 解决了这个问题
    【解决方案4】:

    尝试改用ExtendedFloatingActionBar 并将app:icon 设置为您的FAB 图标,并且不要添加任何文本,EFAB 将看起来像FAB。如果你要将它放在底部操作栏上,我只是不知道如何将摇篮放在 FAB 中。

    【讨论】:

      【解决方案5】:

      使缓存无效并重新启动,然后如果它仍然没有正确显示在布局文件中,则运行应用程序。运行后,它将正确显示在布局文件/预览中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-09
        • 2016-01-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多