悬浮按钮效果演示!
添加依赖:
implementation 'com.google.android.material:material:1.3.0'
xml:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/test_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/play"
app:backgroundTint="#3F51B5"
app:elevation="3dp"
app:pressedTranslationZ="6dp"
app:rippleColor="@color/colorPrimaryDark"
/>
属性:
属性 | 描述 |
---|---|
android:src | 设置图标 |
app:backgroundTint | 设置背景填充色 |
app:elevation | 设置阴影效果 |
app:pressedTranslationZ | 按下时的阴影效果 |
app:rippleColor | 设置点击之后的涟漪颜色 |
处理监听:
FloatingActionButton test_fab = findViewById(R.id.test_fab);
test_fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "floating action button!", Toast.LENGTH_SHORT).show();
}
});