码农进阶之旅Android开发经验谈Android开发

Android《FloatingActionButton》

2017-10-17  本文已影响59人  泅渡者
悬浮按钮

继承:⇐

FloatingActionButton ⇐ VisibilityAwareImageButton ⇐ ImageButton ⇐ ImageView,所以FloatingActionButton拥有ImageView的所有属性。

compile 'com.android.support:design:25.3.1'

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:background="@color/colorPrimary"
    android:layout_height="match_parent"
    >
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@drawable/timg"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fb_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:layout_gravity="left|top"
        android:src="@drawable/icon_menu"
        app:borderWidth="0dp"
        app:backgroundTint="#ff87ffeb"
        app:rippleColor="#33728dff"
        app:elevation="10dp"
        app:pressedTranslationZ="20dp"
        />

</FrameLayout>

属性介绍:

  1. app:borderWidth=""------------------边框宽度,通常设置为0 ,用于解决Android 5.X设备上阴影无法正常显示的问题
  2. app:backgroundTint=""---------------按钮的背景颜色,不设置,默认使用theme中colorAccent的颜色
  3. app:rippleColor=""--------------------点击的边缘阴影颜色
  4. app:elevation=""----------------------边缘阴影的宽度
  5. app:pressedTranslationZ="16dp"-----点击按钮时,按钮边缘阴影的宽度,通常设置比elevation的数值大
    注:5.x设备需要设置一个合理的margin,否则会出现正方形边框。
public class MainActivity extends AppCompatActivity {

    FloatingActionButton floatingActionButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        floatingActionButton = (FloatingActionButton)findViewById(R.id.fb_menu);

        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"你好",Toast.LENGTH_SHORT).show();
            }
        });
    }
}
image.png
FloatingActionButton主要是阴影效果的体现,其实这类组件我们完全可以自己通过shape来定义。
需要代码点击获取
上一篇 下一篇

猜你喜欢

热点阅读