我是 Android Studio 的初学者,使用以下库https://github.com/futuresimple/android-floating-action-button将浮动操作按钮添加到我的项目中,但我不知道该怎么做。请指导我。
3 回答
为 AndroidX 更新:
您可以使用 Google 的 FAB 原生实现:com.google.android.material.floatingactionbutton.FloatingActionButton
依赖:com.google.android.material:material:1.0.0
以前(在 AndroidX 之前):
我建议使用 Google 提供的设计支持浮动操作按钮,如此处所示http://android-developers.blogspot.com/2015/05/android-design-support-library.html 将其添加到您的 gradle 中:
dependencies {
compile 'com.android.support:design:23.0.0'
}
这将包括此处显示的 FloatingActionButton:http: //developer.android.com/reference/android/support/design/widget/FloatingActionButton.html ?utm_campaign=io15&utm_source=dac&utm_medium=blog
使用受支持的库通常比使用第 3 方库更好。
我有点晚了,但是对于那些将寻找如何将浮动操作按钮添加到项目的人来说,由于最近迁移到 androidx 而需要更改项目,这里就是答案。而不是
com.android.support:design
使用 new com.google.android.material:material:1.0.0-rc01
:
dependencies {
implementation 'com.google.android.material:material:1.0.0-rc01'
}
同步项目后,只需在您的活动 xml 文件中声明 FAB:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
android:layout_marginEnd="24dp"/>
只需将依赖项添加到您的 build.gradle:
dependencies {
compile 'com.getbase:floatingactionbutton:1.9.0'
}
要查看按钮是如何添加到您的 xml 布局中的,请查看示例项目。
对于知识:
有两种方法可以使用库:
- 第一种方式,如果库所有者已经在 maven Central 或任何其他存储库上发布了库,那么我们只需要在
build.gradle
文件中使用给定的工件 ID。示例:com.getbase:floatingactionbutton:1.9.0, com.android.support:appcompat-v7:21.0.3 - 另一种方法是在您的项目中引用库项目,就像我们在 Eclipse 中所做的一样。