Android悬浮按钮的使用方法

悬浮按钮效果如下图所示:

步骤1:引用

compile 'com.laocaixw.suspendbuttonlayout:suspendbuttonlayout:1.0.3' 

步骤2:xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
 android:layout_width="match_parent"
 android:orientation="vertical"
 xmlns:suspend="http://schemas.android.com/apk/res-auto"
 xmlns:android="http://schemas.android.com/apk/res/android"> 

 <com.laocaixw.layout.SuspendButtonLayout
  android:id="@+id/layout"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  suspend:distance="80dp"
  suspend:imageSize="50dp"
  suspend:marginY="100dp"
  suspend:number="6"
  suspend:imageMainOpen="@mipmap/suspend_main_open"
  suspend:imageMainClose="@mipmap/suspend_main_close"
  suspend:image1="@mipmap/suspend_1"
  suspend:image2="@mipmap/suspend_2"
  suspend:image3="@mipmap/suspend_3"
  suspend:image4="@mipmap/suspend_4"
  suspend:image5="@mipmap/suspend_5"
  suspend:image6="@mipmap/suspend_6"> 

 </com.laocaixw.layout.SuspendButtonLayout> 

</LinearLayout> 

以上各属性:

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

步骤3:Activity使用

public class MainActivity extends AppCompatActivity { 

 public String[] suspendChildButtonInfo = {"相机", "音乐", "地图", "亮度", "联系人", "短信"}; 

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

  final SuspendButtonLayout suspendButtonLayout = (SuspendButtonLayout) findViewById(R.id.layout);
  suspendButtonLayout.setOnSuspendListener(new SuspendButtonLayout.OnSuspendListener() {
   @Override
   public void onButtonStatusChanged(int status) { 

   } 

   @Override
   public void onChildButtonClick(int index) {
    Toast.makeText(MainActivity.this, "您点击了【"
      + suspendChildButtonInfo[index - 1] + "】按钮!", Toast.LENGTH_SHORT).show();
   }
  }); 

/**  

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

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

  suspendButtonLayout.setMainCloseImageResource(R.mipmap.suspend_main_close); // 设置关闭时,主按钮的图片
  suspendButtonLayout.setMainOpenImageResource(R.mipmap.suspend_main_open); // 设置展开时,主按钮的图片 

  // 设置按钮位置。isRight:true在右边,false在左边;stayPosY:在'按钮停留区域'从上往下,值为从0到100。
  suspendButtonLayout.setPosition(isRight, stayPosY); */ 

 }
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android悬浮按钮点击返回顶部FloatingActionButton

    先看一下Android悬浮按钮点击回到顶部的效果: FloatingActionButton是Design Support库中提供的一个控件,这个控件可以轻松实现悬浮按钮的效果 首先,要在项目中使用这个悬浮按钮就要先把design这个包导入项目 gradle中加入依赖 compile 'com.android.support:design:25.0.0' 接下来就是在xml中使用: 我这里是放置一个listView模拟返回顶部 <?xml version="1.0" encodi

  • Android开发中在TableView上添加悬浮按钮的方法

    如果直接在TableVIewController上贴Button的话会导致这个会随之滚动,下面解决在TableView上实现位置固定悬浮按钮的两种方法: 1.在view上贴tableView,然后将悬浮按钮贴在view的最顶层 2.使用window 首先看一下最终的效果,在tableViewController上添加一个悬浮按钮,该按钮不能随着视图的滚动而滚动 首先介绍上面的第一种方法: 1)创建tableview和底部按钮的属性 //屏幕宽 #define kScreenW [UIScreen

  • Android利用悬浮按钮实现翻页效果

    今天给大家分享下自己用悬浮按钮点击实现翻页效果的例子. 首先,一个按钮要实现悬浮,就要用到系统顶级窗口相关的WindowManager,WindowManager.LayoutParams.那么在AndroidManifest.xml中添加权限: <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> 然后,我们要对WindowManager,WindowManager.Layout

  • Android实现系统级悬浮按钮

    本文实例为大家分享了Android系统级悬浮按钮的具体代码,供大家参考,具体内容如下 具体的需求 1.就是做一个系统级的悬浮按钮,就像iPhone 桌面的那个悬浮按钮效果一样,能随意拖动,并且手一放开,悬浮按钮就自动靠边. 2.可以点击并且可以随意拖动. 3.悬浮按钮自动靠边的时候,或者移动到边上的时候,自动隐藏半边. 4.横竖屏切换都兼容 1.就在WindowManager 里面添加View,这个View通过自定义控件来实现. 2.在onTouch里的MotionEvent.ACTION_MO

  • 圣诞节,写个程序练练手————Android 全界面悬浮按钮实现

    开始我以为悬浮窗口,可以用Android中得PopupWindow 来实现,虽然也实现了,但局限性非常大.比如PopupWindow必须要有载体View,也就是说,必须要指定在那个View的上面来实现.以该View为相对位置,来显示PopupWindow.这就局限了其智能在用户交互的窗口上,相对的显示.而无法自由的拖动位置和在桌面显示. 于是查阅了一些资料,有两种实现方法.一种是自定义Toast,Toast是运行于所有界面之上的,也就是说没有界面可以覆盖它.另一种是Android中得Compat

  • Android开发悬浮按钮 Floating ActionButton的实现方法

    一.介绍 这个类是继承自ImageView的,所以对于这个控件我们可以使用ImageView的所有属性 android.support.design.widget.FloatingActionButton 二.使用准备, 在as 的 build.grade文件中写上 compile 'com.android.support:design:22.2.0' 三.使用说明 xml文件中,注意蓝色字体部分 <android.support.design.widget.FloatingActionButt

  • Android开发模仿qq视频通话悬浮按钮(实例代码)

    模仿qq视频通话的悬浮按钮的实例代码,如下所示: public class FloatingWindowService extends Service{ private static final String TAG="OnTouchListener"; private static View mView = null; private static WindowManager mWindowManager = null; private static Context mContext

  • Android利用WindowManager生成悬浮按钮及悬浮菜单

    简介 本文模仿实现的是360手机卫士基础效果,同时后续会补充一些WindowManager的原理知识. 整体思路 360手机卫士的内存球其实就是一个没有画面的应用程序,整个应用程序的主体是一个Service.我们的程序开始以后,启动一个service,同时关闭activity即可: public class MainActivity extends Activity { private static final String TAG = MainActivity.class.getSimpleN

  • Android自定义可拖拽的悬浮按钮DragFloatingActionButton

    悬浮按钮FloatingActionButton是Android 5.0系统添加的新控件,FloatingActionButton是继承至ImageView,所以FloatingActionButton拥有ImageView的所有属性.本文讲解的是一个实现了可拖拽的悬浮按钮,并为此添加了类似于qq的吸附边框的功能.在此之前,先了解下其简单的使用方式吧: 首先你得添加其依赖 compile 'com.android.support:design:25.3.1' 然后在布局文件中使用. <andro

  • Android中FloatingActionButton实现悬浮按钮实例

    Android中FloatingActionButton(悬浮按钮) 使用不是特别多,常规性APP应用中很少使用该控件. 当然他的使用方法其实很简单.直接上代码: xml: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="

随机推荐