如何在放置在底部应用栏上的 FloatingActionButton (fab) 上方显示 Snackbar(材质设计)

2023-12-13

我正在尝试新材料底部应用栏。在设计指南中,它被告知“在底部应用程序栏和 FAB 上方插入小吃栏或吐司”,但我不确定如何做到这一点。谁能帮我。以下是我正在使用的代码:

主要活动

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(view ->
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show());
}

活动_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </com.google.android.material.appbar.AppBarLayout>

    <include layout="@layout/content_main" />

    <com.google.android.material.bottomappbar.BottomAppBar
        style="@style/Widget.MaterialComponents.BottomAppBar"
        android:id="@+id/bar"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_gravity="bottom"
        android:backgroundTint="@color/colorAccent"
        app:fabAlignmentMode="end" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_dialog_email"
        app:layout_anchor="@id/bar"
        app:layout_anchorGravity="bottom" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

样式.xml

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.MaterialComponents.Dark" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.MaterialComponents.Light" />

gradle

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "..."
        minSdkVersion 22
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility = '1.8'
        targetCompatibility = '1.8'
    }
    buildToolsVersion = '28.0.2'
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
}

问题是每当我点击fab按钮,小吃栏出现在底部应用栏上方,并且还将 fab 推到上方。下面是问题的图片。

material design snackbar plroblem


这是一个issue已修复。有一个新的 APIsetAnchorView我认为我们将在下一次更新中进行;有关详细信息,请前往:https://github.com/material-components/material-components-android/commit/868e80ed6ff183cceeb9c33523bf47f72772c52a

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在放置在底部应用栏上的 FloatingActionButton (fab) 上方显示 Snackbar(材质设计) 的相关文章

  • Android Volley 对于一个请求返回两次结果

    我两天来一直在试图解决这个问题 但我完全被难住了 由于某种原因 我向队列发送一个请求 但 volley 返回两次 这会调用侦听器两次并使列表视图中的结果加倍 我打开了 Volley 的日志记录 可以看到请求被添加到队列中并返回 然后几秒钟后
  • 更改EditText下划线的颜色

    我尝试在以下命令的帮助下更改 EditText 的下划线颜色这个线程 https stackoverflow com questions 34592451 change size of edittext bottom border 我做了同
  • 调整 Android 日期选择器控件的大小

    如何将 android datepicker 控件的初始大小调整为更小或更大 有没有唯一的方法可以重新实现它 我一直无法找到调整其大小的方法 但已经看到了使用 DatePicker 作为对话框的实现 日期呈现为带有编辑按钮的 TextVie
  • Android 中的振动器

    如何创建Vibrator对象并调用vibrate函数 http developer android com reference android os Vibrator html http developer android com refe
  • 运行低于 Lollipop 的设备上的 CameraManager

    我遇到一个问题 我同时使用 Camera 低于 Lollipop 设备 和 CameraManager 类 Lollipop 及以上设备 运行低于 Lollipop 的设备会崩溃 我相信这是因为 import android hardwar
  • 使用意图过滤器从 URL 打开 Android 应用程序不起作用

    我有一个 Android 应用程序 人们用它来替代网站 因此 当用户遇到网站的 URL 时 我想为他们提供在我的应用程序中而不是在浏览器中 打开 URL 的选项 换句话说 我希望出现弹出窗口 让他们在我的应用程序和浏览器 可能还有其他应用程
  • 如何在不将焦点设置到另一个控件的情况下删除焦点?

    我喜欢直观的用户界面 每个屏幕都应该自然 不引人注目地引导用户进入应用程序的下一步 除此之外 我努力让事情尽可能地令人困惑和混乱 只是在开玩笑 我有三个TableRows 每个都包含一个只读且不可聚焦的 EditText 控件 然后在其右侧
  • 如何避免 Android 游戏中的时钟漏洞?

    我需要测量一段可以持续几个小时的时间 我假设执行此操作的正常方法是这样的 Date date new Date wait some time new Date getTime date getTime 但是用户是否可以将 Android 的
  • 更改密码 将重力切换到左侧(对于 RTL 语言)

    I use TextInputLayout从用户那里获取密码 如下所示
  • 为什么mapbox-android sdk 无法从build.gradle 解析?

    我试图在这里设置基本教程 但我被 Android Studio 中的错误阻止 https docs mapbox com android maps overview install the maps sdk https docs mapbo
  • 如何在android.mk文件中指定c头文件

    我有一个 c 源文件夹名称 clib 其中 我有一些示例文件 例如 1 h 1 c 2 h 2 c 3 c 3 h 在该文件夹之外我有 4 个 h 4 c 4 jni h 4 jni c 现在为了构建 so 我创建了像这样的 android
  • 如何判断手机是否有刘海

    如果存在缺口 我需要修改应用程序的工具栏 现在 该凹口隐藏了工具栏中的一些内容 if Build VERSION SDK INT Build VERSION CODES LOLLIPOP Build VERSION SDK INT Buil
  • 在 RecyclerView 中自动调整 TextView 大小会导致文本大小减小

    我正在尝试使用自动调整文本视图大小 https developer android com guide topics ui look and feel autosizing textview在 RecyclerView 中 但是当我滚动几次
  • 在一天中的特定时间设置闹钟

    我正在尝试将闹钟设置在一天中的特定时间 例如 20 15 这是我正在使用的代码 但它不会在 20 15 关闭 Intent intent new Intent AlarmActivity this MyBroadcastReceiver c
  • Android 视图可见性消失

    如果我设置了一个视图可见性 它被夸大了 消失了 它会加快我的 UI 速度吗 它将加快 UI 的实际绘制速度 因为 好吧 您不必再绘制它 但它仍然会膨胀 并且膨胀过程不会更快
  • TextView 中缩进项目符号列表

    我有一个 TextView 我用 strings xml 中的字符串资源中的文本填充它 字符串资源包含 元素 用于在 TextView 内创建项目符号列表 我的问题是我想控制项目符号列表中跨越多行的行的缩进 默认情况下 文本不会缩进超过项目
  • Ctrl 单击 Eclipse 中的类显示二进制/jar 而不是源

    使用Eclipse 版本 Helios 服务版本 2 内部版本号 20110218 0911 与Android插件 我有一个 Android 项目 AndroidUtil 它被标记为我的主要 Android 应用程序的 库 项目 当我按住
  • Retrofit RxAndroid 为 api 调用制作包装器

    我想在改造中为 api 调用创建一个包装器 这样我就可以在常见位置显示 ProgressDialog 并处理常见响应 我通过创建这样的包装器来实现这一点 public static
  • SpannableString 中的 URLSpan

    我正在使用这个例子SpannableString http developer android com resources samples ApiDemos src com example android apis text Link ht
  • Android Studio安装JDK错误

    In Android Studio I am facing bellow error 当我按下时会显示此弹出窗口Alt Enter对于缺少的类 符号 当我点击 setup SDK 时 它显示两个选项 1 8 Java版本 1 8 0 60

随机推荐