介绍

Source Code中的介绍如下:

Floating action buttons are used for a special type of promoted action. They are distinguished

by a circled icon floating above the UI and have special motion behaviors related to morphing,

launching, and the transferring anchor point.

Floating action buttons come in two sizes: the default and the mini. The size can be

controlled with the {@code fabSize} attribute.

As this class descends from {@link ImageView}, you can control the icon which is displayed

via {@link # setImageDrawable(Drawable)}.

The background color of this view defaults to the your theme’s {@code colorAccent}. If you

wish to change this at runtime then you can do so via

{@link # setBackgroundTintList(ColorStateList)}.

@attr ref android.support.design.R.styleable#FloatingActionButton_fabSize

属性值

属性值 意义
app:backgroundTint 设置背景颜色
app:fabSize 设置FAB的大小,主要有两个取值normal,mini
android:src 设置FAB的图标内容
app:rippleColor 设置FAB点击过程中的颜色
app:elevation 设置FAB正常情况下的阴影效果
app:pressedTranslationZ 设置FAB点击时的阴影大小
app:borderWidth 设置边框宽度
android:clickable 是否可点击true or false
app:layout_anchor 设置FAB的锚点,即以哪个控件为参照点设置位置
app:layout_anchorGravity 设置FAB相对锚点的位置,取值:top,bottom.left,right,center_vertical,fill_vertical, center_horizontal,fill_horizontal,center,fill,clip_vertical,clip_horizontal,start,end

具体使用

布局文件

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark"
android:minHeight="?attr/actionBarSize"
app:navigationIcon="@drawable/ic_account_balance_black_24dp"
app:title="Mraz FAB Demo"></android.support.v7.widget.Toolbar> <android.support.design.widget.FloatingActionButton
android:id="@+id/fab_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|bottom"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:clickable="true" android:onClick="leftClick"
android:src="@drawable/ic_arrow_back_black_24dp"
app:backgroundTint="@color/colorPrimary"
app:fabSize="mini"
app:rippleColor="@color/colorPress" /> <android.support.design.widget.FloatingActionButton
android:id="@+id/fab_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:clickable="true"
android:onClick="rightClick"
android:src="@drawable/ic_arrow_forward_black_24dp"
app:backgroundTint="@color/colorLight"
app:borderWidth="0dp"
app:elevation="20dp"
app:fabSize="normal"
app:pressedTranslationZ="50dp"
app:rippleColor="@color/colorPress" /> <android.support.design.widget.FloatingActionButton
android:id="@+id/fab_anchor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp" android:clickable="true"
android:onClick="topClick"
android:src="@drawable/ic_arrow_forward_black_24dp"
app:backgroundTint="#ff87ffeb"
app:borderWidth="0dp"
app:elevation="6dp"
app:fabSize="normal"
app:layout_anchor="@+id/toolbar"
app:layout_anchorGravity="right"
app:pressedTranslationZ="12dp"
app:rippleColor="#33728dff" />
</android.support.design.widget.CoordinatorLayout>

布局中一共设置了3个FAB,一个在左下角,一个在右下角,一个设置了锚点,颜色可以自己调整,简单的用法就是这个样子,对应的onClick事件在MainActivity中实现。

代码文件

MainActivity.java

package mraz.com.appbardemo;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View; public class MainActivity extends AppCompatActivity { Toolbar toolbar; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
} @TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void leftClick(View view) {
toolbar.setTitle("Left FAB onClick");
} public void rightClick(View view) {
toolbar.setTitle("Right FAB onClick");
} public void topClick(View view) {
toolbar.setTitle("Top FAB onClick");
}
}

实际效果

<Android 基础(十)> FloatingActionButton的更多相关文章

  1. 安卓Design包下的TextInputLayout和FloatingActionButton的简单使用

    终于介绍到Design包的最后的东西了. 也很简单,一个是TextInputLayout. TextInputLayout作为一个父容器,包含一个新的EditText,可以给EditText添加意想不 ...

  2. 关于FloatingActionButton

    由于FloatingActionButton本质上是ImageView,跟ImageView相关的就不介绍,这里重点介绍新加的几个属性. app:fabSize:FloatingActionButto ...

  3. 浅谈FloatingActionButton(悬浮按钮)

    一.介绍 这个类是继承自ImageView的,所以对于这个控件我们可以使用ImageView的所有属性 android.support.design.widget.FloatingActionButt ...

  4. 如何自定义FloatingActionButton的大小

    Google最近为了让开发者更好的更规范的应用Material Design设计思想,特意放出了android support design library,里面含有更多Material Design ...

  5. 【转】Android M新控件之FloatingActionButton,TextInputLayout,Snackbar,TabLayout的使用

    Android M新控件之FloatingActionButton,TextInputLayout,Snackbar,TabLayout的使用 分类: Android UI2015-06-15 16: ...

  6. Android FloatingActionButton(FAB) 悬浮按钮

    FloatingActionButton 悬浮按钮                                                                            ...

  7. FloatingActionButton增强版,一个按钮跳出多个按钮--第三方开源--FloatingActionButton

      FloatingActionButton项目在github上的主页:https://github.com/futuresimple/android-floating-action-button F ...

  8. Android Material Design的FloatingActionButton,Snackbar和CoordinatorLayout

    如果是为了兼容低版本的Android系统,则需要引用Android Material Design的扩展支持库,我在之前的一篇文章张,较为详细的说明了如何导入Android Material Desi ...

  9. 伴随ListView、RecyclerView、ScrollView滚动滑入滑出小图标--第三方开源--FloatingActionButton

    FloatingActionButton在github上的项目主页是:https://github.com/makovkastar/FloatingActionButton 它的依赖包NineOldA ...

  10. FloatingActionButton的一点学习感悟

    最近在学习android材料设计的新控件,前面一篇文章讲到 CoordinatorLayout 结合几个新控件可以实现的几个效果.其中第一个是,Coordinatorlayout + Floating ...

随机推荐

  1. Django个人博客开发 | 前言

    本渣渣不专注技术,只专注使用技术,不是一个资深的coder,是一个不折不扣的copier 1.前言 自学 Python,始于 Django 框架,Scrapy 框架,elasticsearch搜索引擎 ...

  2. 数据结构4:顺序表(线性表的顺序存储结构)及C语言实现

    逻辑结构上呈线性分布的数据元素在实际的物理存储结构中也同样相互之间紧挨着,这种存储结构称为线性表的顺序存储结构. 也就是说,逻辑上具有线性关系的数据按照前后的次序全部存储在一整块连续的内存空间中,之间 ...

  3. mongoDB3.4安装

    添加官方yum源——mongodb3.4vim /etc/yum.repos.d/mongodb-org-3.4.repo [mongodb-org-3.4] name=MongoDB Reposit ...

  4. pytorch实现squeezenet

    squeezenet是16年发布的一款轻量级网络模型,模型很小,只有4.8M,可用于移动设备,嵌入式设备. 关于squeezenet的原理可自行阅读论文或查找博客,这里主要解读下pytorch对squ ...

  5. php 中 ?? 和 empty 的 区别

    left??right  操作符...当 left 为空时 返回 right ..注意 和 empty 相比 ,空字符,0,'0' 都会返回它本身,而不是 右边的..也就是 当且仅当 没有设置变量或者 ...

  6. poj2002 数正方形 (哈希+几何)

    题目传送门 题目大意:给你一堆点,问你能组成几个正方形. 思路:一开始想的是用对角线的长度来当哈希的key,但判断正方形会太复杂,然后就去找了一下正方形的判断方法,发现 已知: (x1,y1) (x2 ...

  7. 5.SpringMVC

    1.SpringMVC概述 概述: SpringMVC是基于请求驱动,围绕一个核心Servlet 转发请求到对应的Controller而设计的优点:是一个典型的教科书式的MVC构架,易学易用提供了清晰 ...

  8. CSS background 属性全家桶

    介绍我们都知道css的background属性是一个复合属性,可以简写成一行代码,也可以将每个属性分开来写. background 简写属性在一个声明中设置所有的背景属性.如:body{ backgr ...

  9. ios中页面底部输入框,position:fixed元素的问题

    在安卓上点击页面底部的输入框,软键盘弹出,页面移动上移.ios上,软件盘弹出,输入框看不到了.让他弹出时让滚动条在最低部 var u = navigator.userAgent, app = navi ...

  10. php rtrim的一个坑,很“二”的问题

    一.背景: 做项目的时候遇到一种情况,商家的营业时间显示的问题,设计图上要求显示成:“星期一,星期二,星期三,星期四,星期五,星期六,星期天” 换句话说,就是营业星期勾哪个就显示哪个,类似下面这样: ...