android悬浮按钮demo记录

1.导包,框架来源:https://github.com/futuresimple/android-floating-action-button/blob/master/sample/src/main/res/layout/activity_main.xml

dependencies {
    implementation 'com.getbase:floatingactionbutton:1.10.0'
  
}

2.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout //布局随意
    xmlns:fab="http://schemas.android.com/apk/res-auto" //fab的包一定要写
    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">

    <com.getbase.floatingactionbutton.FloatingActionsMenu
        android:id="@+id/multiple_actions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        fab:fab_addButtonColorNormal="@color/white"
        fab:fab_addButtonColorPressed="@color/white_pressed"
        fab:fab_addButtonPlusIconColor="@color/half_black" //背景颜色
        fab:fab_labelStyle="@style/menu_labels_style"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp">

        <com.getbase.floatingactionbutton.FloatingActionButton
            android:id="@+id/action_a"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            fab:fab_icon="@mipmap/ic_launcher_more_info" //添加背景图片
            fab:fab_colorNormal="@color/white"
            fab:fab_colorPressed="@color/white_pressed"
            fab:fab_title="Action A" />

        <com.getbase.floatingactionbutton.FloatingActionButton
            android:id="@+id/action_b"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            fab:fab_icon="@mipmap/ic_launcher_background"
            fab:fab_colorNormal="@color/white"
            fab:fab_colorPressed="@color/white_pressed"
            fab:fab_title="Action with a very long name that won\'t fit on the screen" />

    </com.getbase.floatingactionbutton.FloatingActionsMenu>






</RelativeLayout>

设置按钮尺寸和弹出方向,默认应是向上

fab:fab_addButtonSize="mini"
fab:fab_expandDirection="down"

3.activity和监听

public class SecondActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        initButtons();
    }

    public void initButtons() {
        FloatingActionButton actionButton = findViewById(R.id.action_a);
        FloatingActionButton button = findViewById(R.id.action_b);

        actionButton.setOnClickListener(new buttonClick());//给按钮添加监听
        button.setOnClickListener(new buttonClick());

    }
    //监听类
    public class buttonClick implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.action_a:
                    System.out.println("点了个a");
                    break;
                case R.id.action_b:
                    System.out.println("点了个b");
                    break;
                default:
                    break;
            }
        }
    }

}

4.效果图

android悬浮按钮demo记录android悬浮按钮demo记录