悬浮按钮和可交互提示

FloatingActionButton是Design Support库中提供的一个控件,可以比较轻松的实现悬浮按钮的效果。

效果图:

1.修改activity_main.xml中的代码,如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.design.widget.CoordinatorLayout
        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="?attr/actionBarSize"
            android:background="#009999"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
        </android.support.v7.widget.Toolbar>
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="16dp"
            app:borderWidth="0dp"
            android:background="#009999"
            app:fabSize="normal"
            android:backgroundTint="#009999"
            android:src="@drawable/write"/>
        <!--android:src="@android:drawable/ic_dialog_email"-->
            <!--/>-->
    </android.support.design.widget.CoordinatorLayout>
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/nav_menu"
        app:headerLayout="@layout/nav_header">
    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

可以看到,在主屏幕中加入了一个FloatingActionButton。backgroundTint属性修改悬浮按钮的背景色,APP:elevation=“8dp”可以设置悬浮按钮的高度。

  • 对于背景颜色可以这样:
app:backgroundTint="@color/mycolor"
  • 而ripple则可以这样:
app:rippleColor="@android:color/white"
  • 还可以在java代码中调用相关方法,不过backgroundTint稍微麻烦点,因为它使用的是state list,下面的具体做法(感谢Carlos Morera的提示):
fab.setBackgroundTintList(ColorStateList.valueOf("Your color"));
  • 有两种大小的浮动操作按钮,要得到mini版的fab耶很简单,添加一个属性就是:
app:fabSize="mini"

2.悬浮按钮的点击事件,修改MainActivity中的代码:

 //FloatingActionButton的点击事件
    @OnClick(R.id.fab)
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.fab:
                Snackbar.make(view,"Data deleted",Snackbar.LENGTH_SHORT)
                        .setAction("Undo", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Toast.makeText(MainActivity.this,"Date restored",
                                        Toast.LENGTH_SHORT).show();
                            }
                }).show();
                break;
        }
    }

上述代码实现了FloatingActivityButton的点击事件,同时使用了更先进的提示工具—Snackbar。Snackbar的用法也非常简单,它和toast基本相似,只不过可以额外增加一个按钮的点击事件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值