先看效果
说下它的配置:在app/build.gradle 添加以下代码依赖:
implementation 'com.android.support:design:28.0.0'
implementation 'com.getbase:floatingactionbutton:1.10.1'
title 字体以及颜色的设置:
FloatingActionButton默认无法显示文字的情况,所以这里需要对其配置样式;
在res/value/style中添加:
<!--Here is the style of floatingactionbutton's title-->
<style name="floatingActionsMenu_fab_style">
<item name="android:background">@drawable/fab_label_background</item> //文字背景的样式
<item name="android:textColor">@color/text_color</item> //文字的颜色
</style>
这里是我的res/value/color的配置:
<!--floatingactionbutton's coclor-->
<color name="white">#ffffff</color>
<color name="text_color">#000000</color>
这里我们在drawable中添加自定义消灭了文件用于配置文字样式:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 填充的颜色 -->
<solid android:color="#FFffffff" />
<!-- 设置按钮的四个角为弧形 -->
<!-- android:radius 弧形的半径 -->
<corners android:radius="25dip" />
<!-- padding:Button里面的文字与Button边界的间隔 -->
<padding
android:bottom="15dp"
android:left="20dp"
android:right="20dp"
android:top="15dp" />
<!--设置描边-->
<stroke
android:width= "10dp"
android:color= "#00000000" />
</shape>
完成这些步骤之后只需在布局文件中调用即可:
这里是我的布局文件:
<com.getbase.floatingactionbutton.FloatingActionsMenu
android:id="@+id/multiple_actions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fab:fab_expandDirection="down"
fab:fab_labelStyle="@style/floatingActionsMenu_fab_style">
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="@+id/action_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fab:fab_icon="@drawable/img_addhuanzhe"
fab:fab_size="mini"
fab:fab_title="按钮一" />
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="@+id/action_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fab:fab_icon="@drawable/img_addhuanzhe"
fab:fab_size="mini"
fab:fab_title="按钮二" />
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="@+id/action_c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fab:fab_icon="@drawable/img_addhuanzhe"
fab:fab_size="mini"
fab:fab_title="按钮三" />
</com.getbase.floatingactionbutton.FloatingActionsMenu>
FloatingActionButton基本使用:
/*
设置三个悬浮按钮的监听事件
*/
//
final FloatingActionButton actionA = (FloatingActionButton) findViewById(R.id.action_a);
actionA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//空
}
});
//跳转到 FromPointToPoint 活动
final FloatingActionButton actionB = (FloatingActionButton) findViewById(R.id.action_b);
actionB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivityForResult(new Intent(MainActivity.this,FromPointToPoint.class),0x1);
}
});
//弹出提示
final FloatingActionButton actionC = (FloatingActionButton) findViewById(R.id.action_c);
actionC.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"dianjile",Toast.LENGTH_SHORT).show();
}
});