效果图
右下角那个粉色的圈圈
第一步
因为这个控件是Design Support提供的,所以需要将这个库引入到项目中.
如果版本号报错,你可以将鼠标放在这句代码上,看它提示你的版本号是多少,改成你的就好了
compile 'com.android.support:design:27.1.1'
第二步
layout文件引用,其中的elevation是给悬浮窗指定一个高度值,高度值越大,投影范围也越大,我引入了自己的图片,看起来像是刷新的效果
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:id="@+id/my_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="16dp"
android:src="@drawable/myimg"
app:elevation="8dp" />
</RelativeLayout>
点击事件
其实和普通的Button是一样的
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatingActionButton my_fab = (FloatingActionButton) findViewById(R.id.my_fab);
my_fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//处理按钮点击后的操作...
}
});
}
}