您当前的位置: 首页 > 

一个带浮动按钮的布局

发布时间:2016-11-18 20:54:49 ,浏览量:0

SuspendButtonLayout
项目地址: laocaixw/SuspendButtonLayout
简介:一个带浮动按钮的布局
一个带浮动按钮的布局,按钮可以展开
一.简介

一个轻量的带浮动按钮的布局。按钮可以随意展开关闭、显示隐藏,可以监听按钮打开、关闭、移动等状态,可以监听各个按钮的点击事件。无论你怎么拖动按钮,按钮最终都会停留在左边缘或右边缘。

二.效果图

image

三.Demo 的 Apk 下载

Apk

四.用法

在 app 的 build.gradle 文件中添加依赖:

dependencies {
    compile 'com.laocaixw.suspendbuttonlayout:suspendbuttonlayout:1.0.0'
}

这边前提是,在工程目录下引入 jcenter(目前的 Android Studio 版本基本都默认引入了):

repositories {
    jcenter()
}

因为SuspendButtonLayout布局继承了 RelativeLayout 布局,且按钮在SuspendButtonLayout中,所以想让按钮浮在你的布局上方,应该把SuspendButtonLayout布局写在你的布局下面。(如果你无所谓按钮在什么位置,可以直接把SuspendButtonLayout当做 RelativeLayout 来用。)

1.布局:

			

如下图,绿色部分为按钮停留区域,各属性:

  • distance="80dp" // 按钮打开后,主按钮和子按钮的距离
  • imageSize="50dp" // 按钮大小,所占区域的边长
  • marginY="100dp" // 与上下边缘距离,下图中黄色部分的高度
  • number="6" // 展开的子按钮的数量,可以是 3-6 个
  • imageMainOpen="@mipmap/suspendMainOpen" // 中间按钮展开时的图片资源
  • imageMainClose="@mipmap/suspendMainClose" // 中间按钮关闭时的图片资源
  • image1="@mipmap/suspend_1" // 子按钮的图片资源,image1~image6

image

2.使用方法:
SuspendButtonLayout suspendButtonLayout = (SuspendButtonLayout) findViewById(R.id.layout);

suspendButtonLayout.setOnSuspendListener(new SuspendButtonLayout.OnSuspendListener() {
    @Override
    public void onButtonStatusChanged(int status) {
        // 监听按钮状态:展开、关闭、移动等
    }

    @Override
    public void onChildButtonClick(int index) {
        // 监听子按钮的点击事件
    }
});

suspendButtonLayout.hideSuspendButton(); // 隐藏按钮
suspendButtonLayout.showSuspendButton(); // 显示按钮

suspendButtonLayout.openSuspendButton(); // 展开按钮
suspendButtonLayout.closeSuspendButton(); // 关闭按钮

suspendButtonLayout.setMainCloseImageResource(R.mipmap.suspend_main_close); //设置关闭时,主按钮的图片
suspendButtonLayout.setMainOpenImageResource(R.mipmap.suspend_main_open); //设置展开时,主按钮的图片
3.详细用法见 Demo。 五.原理
  1. SuspendButtonLayout 布局继承了 RelativeLayout,拥有 RelativeLayout 的一切属性,每个按钮都在 SuspendButtonLayout 中;
  2. 按钮的拖动是在 onTouch 方法中对控件做 Animator 动画处理;
  3. 悬浮按钮 OnSuspendListener 事件监听采用接口回调机制。
关注
打赏
1688896170
查看更多评论

暂无认证

  • 0浏览

    0关注

    110950博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.1577s