floating_button_layout.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <ImageButton
android:id="@+id/ImageButton_Floating"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="15dp"
android:layout_marginRight="15dp"
android:background="@drawable/floating_button_style"
android:contentDescription="@null" >
</ImageButton> </RelativeLayout>

floating_menu.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <Button
android:id="@+id/Button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="选项1" /> <Button
android:id="@+id/Button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="选项2" /> <Button
android:id="@+id/Button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="选项3" /> <Button
android:id="@+id/Button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="选项4" /> </LinearLayout>

FloatingMenu.java

 package com.wangzhen.view;

 import com.wangzhen.animation.R;

 import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast; /**
* 漂浮菜单
*
* @author Administrator
*
*/
public class FloatingMenu extends PopupWindow { private Context mContext;
private View view;
private Button Button1;
private Button Button2;
private Button Button3;
private Button Button4; public FloatingMenu(Context context) {
mContext = context;
LayoutInflater mInflater = LayoutInflater.from(mContext);
view = mInflater.inflate(R.layout.floating_menu, null);
Button1 = (Button) view.findViewById(R.id.Button1);
Button2 = (Button) view.findViewById(R.id.Button2);
Button3 = (Button) view.findViewById(R.id.Button3);
Button4 = (Button) view.findViewById(R.id.Button4); Button1.setOnClickListener(new MyClick());
Button2.setOnClickListener(new MyClick());
Button3.setOnClickListener(new MyClick());
Button4.setOnClickListener(new MyClick()); setWidth(300);
setHeight(LayoutParams.WRAP_CONTENT);
setFocusable(true);
ColorDrawable drawable = new ColorDrawable(0xb000000);
setBackgroundDrawable(drawable);
setContentView(view);
} private void ShowToast(String string) {
Toast.makeText(mContext, string, Toast.LENGTH_SHORT).show();
} class MyClick implements OnClickListener { @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.Button1:
ShowToast("Button1");
break;
case R.id.Button2:
ShowToast("Button2");
break;
case R.id.Button3:
ShowToast("Button3");
break;
case R.id.Button4:
ShowToast("Button4");
break;
default:
break;
}
dismiss();
} }
}

FloatingButtonActivity.java

 package com.wangzhen.animation;

 import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.wangzhen.view.FloatingMenu;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton; @ContentView(R.layout.floating_button_layout)
public class FloatingButtonActivity extends ActionBarActivity { private Context mContext; @ViewInject(R.id.ImageButton_Floating)
private ImageButton ImageButton_Floating; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ViewUtils.inject(this);
mContext = this;
ActionBar mActionBar = getSupportActionBar();
mActionBar.setDisplayHomeAsUpEnabled(true);
mActionBar.setDisplayShowHomeEnabled(false);
} @OnClick({ R.id.ImageButton_Floating })
private void OnClick(View view) {
switch (view.getId()) {
case R.id.ImageButton_Floating:
FloatingMenu menu = new FloatingMenu(mContext);
menu.setFocusable(true);
menu.setOutsideTouchable(true);
View view_btn = findViewById(R.id.ImageButton_Floating);
menu.showAtLocation(view_btn, Gravity.BOTTOM | Gravity.RIGHT, 0,
view_btn.getHeight() + 30);
break; default:
break;
}
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
FinishActivity();
break; default:
break;
}
return super.onOptionsItemSelected(item);
} /**
* 退出Activity
*/
private void FinishActivity() {
finish();
overridePendingTransition(0, R.anim.anim_page_out);
} @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
FinishActivity();
}
return false;
}
}

自定义悬浮按钮:FloatingButton的更多相关文章

  1. (IOS)悬浮按钮Demo

    思路:传入一个底层的view,将悬浮按钮(用view实现)和展开的子按钮列表add在其上,子按钮列表开始将坐标和悬浮按钮对应好后先将其隐藏,悬浮按钮识别到tap手势后触发展示子按钮列表的方法.通过在t ...

  2. android悬浮按钮(Floating action button)的两种实现方法

    原文: http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1028/1857.html 最近android中有很多新的设计规范被引入 ...

  3. 在TableView上添加悬浮按钮

    如果直接在TableVIewController上贴Button的话会导致这个会随之滚动,下面解决在TableView上实现位置固定悬浮按钮的两种方法: 1.在view上贴tableView,然后将悬 ...

  4. Mono自定义图片按钮

    首先,我们编写一个MyImageButton类,继承自LinearLayout public class MyPhoneImageButton:LinearLayout { private Image ...

  5. android自定义控件(3)-自定义当前按钮属性

    那么还是针对我们之前写的自定义控件:开关按钮为例来说,在之前的基础上,我们来看看有哪些属性是可以自定义的:按钮的背景图片,按钮的滑块图片,和按钮的状态(是开还是关),实际上都应该是可以在xml文件中直 ...

  6. iOS 自定义返回按钮,保留系统滑动返回

    原文链接 自定义返回按钮保留系统滑动返回手势.gif 1.简介 使用苹果手机,最喜欢的就是用它的滑动返回.作为一个开发者,我们在编写很多页面的时候,总是会因为这样那样的原因使得系统的滑动返回不可用.使 ...

  7. Android FloatingActionButton(FAB) 悬浮按钮

    FloatingActionButton 悬浮按钮                                                                            ...

  8. easyUI——datebox验证和自定义取消按钮

    来源:http://blog.csdn.net/liusong0605/article/details/42270463 1. datebox验证        验证结束时间<起始时间: 起始时 ...

  9. iOS 7 自定义Back按钮 与 Pop interactive gesture 问题

    1.自定义Back按钮 iOS中很多时候我们都会自定义返回按钮,也是一件easy的事,类似如下: // 返回按钮 1 - (void)showNavBackButton { UIButton *bac ...

随机推荐

  1. ext中处理Combobox组件点击触发后台事件的问题

    ext的Combobox组件在绑定数据的时候需要一个Store来绑定数据,在store里面我们可以设置autoLoad属性,这个属性表示Store可以自动的到后台获取数据,ext实质上就是封装好的ja ...

  2. cocos2d-x 工程目录结构说明

    下载最新的cocos2d-x,解压完之后呈现出如下的目录结构: cocos2dx:框架核心目录,里面是存放了2dx引擎的核心代码. CocosDenshion:各个平台的音效实现. document: ...

  3. Java 的简单了解

    本文是主要根据百度百科以网上一些资料,整理的一点对Java的浅显的了解,不当之处,还请大家批评指正. 最初见到Java这个单词,是在以前的手机上,游戏启动时总会显示java的图标和名字,就感觉java ...

  4. php把数组保存成文件格式

    php把数组保存为文件格式的函数实例,或许有的还没听说过可以把数组保存成文件,其实这样做也是另有它用的,两种方法各有千秋,有兴趣的PHP爱好者敬请参阅: $file="./cache/fil ...

  5. 在同一上机器上建立两个SVN服务

    最快的三步: 1,SVNADMIN CREATE NAME 2, 改写CONF目录下的相关三个文件. 3,重写一个SVN的启动脚本,指定这个SVN不同的目录及端口号. [general] ### Th ...

  6. Qt源码分析之信号和槽机制

    Qt的信号和槽机制是Qt的一大特点,实际上这是和MFC中的消息映射机制相似的东西,要完成的事情也差不多,就是发送一个消息然后让其它窗口响应,当然,这里的消息是广义的说法,简单点说就是如何在一个类的一个 ...

  7. Oracle extent

    extent 上的块 物理上绝对连续 多个连续的block组成一个extent 不连续怎么多块读? Oracle 给表分配的单位是extent 去做 全表扫描的时候 以块为单位,按照extent来读取 ...

  8. Qt入门(5)——用Qt控件创建一个电话本界面

    具体实现步骤: 一.首先用 Qt Designer 创建一个两张图的对话框,分别保存为listdialog.ui和editdialog.ui文件 要注意其中各个空间对应的名称修改好 二.新建一个Qt应 ...

  9. Linux系统编程(28)——线程间同步

    多个线程同时访问共享数据时可能会冲突,这跟前面讲信号时所说的可重入性是同样的问题.比如两个线程都要把某个全局变量增加1,这个操作在某平台需要三条指令完成: 从内存读变量值到寄存器 寄存器的值加1 将寄 ...

  10. BZOJ1176---[Balkan2007]Mokia (CDQ分治 + 树状数组)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1176 CDQ第一题,warush了好久.. CDQ分治推荐论文: 1 <从<C ...