floating_button_layout.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent" >
  5.  
  6. <ImageButton
  7. android:id="@+id/ImageButton_Floating"
  8. android:layout_width="50dp"
  9. android:layout_height="50dp"
  10. android:layout_alignParentBottom="true"
  11. android:layout_alignParentRight="true"
  12. android:layout_marginBottom="15dp"
  13. android:layout_marginRight="15dp"
  14. android:background="@drawable/floating_button_style"
  15. android:contentDescription="@null" >
  16. </ImageButton>
  17.  
  18. </RelativeLayout>

floating_menu.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6.  
  7. <Button
  8. android:id="@+id/Button1"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:text="选项1" />
  12.  
  13. <Button
  14. android:id="@+id/Button2"
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. android:text="选项2" />
  18.  
  19. <Button
  20. android:id="@+id/Button3"
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content"
  23. android:text="选项3" />
  24.  
  25. <Button
  26. android:id="@+id/Button4"
  27. android:layout_width="match_parent"
  28. android:layout_height="wrap_content"
  29. android:text="选项4" />
  30.  
  31. </LinearLayout>

FloatingMenu.java

  1. package com.wangzhen.view;
  2.  
  3. import com.wangzhen.animation.R;
  4.  
  5. import android.content.Context;
  6. import android.graphics.drawable.ColorDrawable;
  7. import android.view.LayoutInflater;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.view.WindowManager.LayoutParams;
  11. import android.widget.Button;
  12. import android.widget.PopupWindow;
  13. import android.widget.Toast;
  14.  
  15. /**
  16. * 漂浮菜单
  17. *
  18. * @author Administrator
  19. *
  20. */
  21. public class FloatingMenu extends PopupWindow {
  22.  
  23. private Context mContext;
  24. private View view;
  25. private Button Button1;
  26. private Button Button2;
  27. private Button Button3;
  28. private Button Button4;
  29.  
  30. public FloatingMenu(Context context) {
  31. mContext = context;
  32. LayoutInflater mInflater = LayoutInflater.from(mContext);
  33. view = mInflater.inflate(R.layout.floating_menu, null);
  34. Button1 = (Button) view.findViewById(R.id.Button1);
  35. Button2 = (Button) view.findViewById(R.id.Button2);
  36. Button3 = (Button) view.findViewById(R.id.Button3);
  37. Button4 = (Button) view.findViewById(R.id.Button4);
  38.  
  39. Button1.setOnClickListener(new MyClick());
  40. Button2.setOnClickListener(new MyClick());
  41. Button3.setOnClickListener(new MyClick());
  42. Button4.setOnClickListener(new MyClick());
  43.  
  44. setWidth(300);
  45. setHeight(LayoutParams.WRAP_CONTENT);
  46. setFocusable(true);
  47. ColorDrawable drawable = new ColorDrawable(0xb000000);
  48. setBackgroundDrawable(drawable);
  49. setContentView(view);
  50. }
  51.  
  52. private void ShowToast(String string) {
  53. Toast.makeText(mContext, string, Toast.LENGTH_SHORT).show();
  54. }
  55.  
  56. class MyClick implements OnClickListener {
  57.  
  58. @Override
  59. public void onClick(View v) {
  60. switch (v.getId()) {
  61. case R.id.Button1:
  62. ShowToast("Button1");
  63. break;
  64. case R.id.Button2:
  65. ShowToast("Button2");
  66. break;
  67. case R.id.Button3:
  68. ShowToast("Button3");
  69. break;
  70. case R.id.Button4:
  71. ShowToast("Button4");
  72. break;
  73. default:
  74. break;
  75. }
  76. dismiss();
  77. }
  78.  
  79. }
  80. }

FloatingButtonActivity.java

  1. package com.wangzhen.animation;
  2.  
  3. import com.lidroid.xutils.ViewUtils;
  4. import com.lidroid.xutils.view.annotation.ContentView;
  5. import com.lidroid.xutils.view.annotation.ViewInject;
  6. import com.lidroid.xutils.view.annotation.event.OnClick;
  7. import com.wangzhen.view.FloatingMenu;
  8. import android.content.Context;
  9. import android.os.Bundle;
  10. import android.support.v7.app.ActionBar;
  11. import android.support.v7.app.ActionBarActivity;
  12. import android.view.Gravity;
  13. import android.view.KeyEvent;
  14. import android.view.MenuItem;
  15. import android.view.View;
  16. import android.widget.ImageButton;
  17.  
  18. @ContentView(R.layout.floating_button_layout)
  19. public class FloatingButtonActivity extends ActionBarActivity {
  20.  
  21. private Context mContext;
  22.  
  23. @ViewInject(R.id.ImageButton_Floating)
  24. private ImageButton ImageButton_Floating;
  25.  
  26. @Override
  27. protected void onCreate(Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29. ViewUtils.inject(this);
  30. mContext = this;
  31. ActionBar mActionBar = getSupportActionBar();
  32. mActionBar.setDisplayHomeAsUpEnabled(true);
  33. mActionBar.setDisplayShowHomeEnabled(false);
  34. }
  35.  
  36. @OnClick({ R.id.ImageButton_Floating })
  37. private void OnClick(View view) {
  38. switch (view.getId()) {
  39. case R.id.ImageButton_Floating:
  40. FloatingMenu menu = new FloatingMenu(mContext);
  41. menu.setFocusable(true);
  42. menu.setOutsideTouchable(true);
  43. View view_btn = findViewById(R.id.ImageButton_Floating);
  44. menu.showAtLocation(view_btn, Gravity.BOTTOM | Gravity.RIGHT, 0,
  45. view_btn.getHeight() + 30);
  46. break;
  47.  
  48. default:
  49. break;
  50. }
  51. }
  52.  
  53. @Override
  54. public boolean onOptionsItemSelected(MenuItem item) {
  55. switch (item.getItemId()) {
  56. case android.R.id.home:
  57. FinishActivity();
  58. break;
  59.  
  60. default:
  61. break;
  62. }
  63. return super.onOptionsItemSelected(item);
  64. }
  65.  
  66. /**
  67. * 退出Activity
  68. */
  69. private void FinishActivity() {
  70. finish();
  71. overridePendingTransition(0, R.anim.anim_page_out);
  72. }
  73.  
  74. @Override
  75. public boolean onKeyDown(int keyCode, KeyEvent event) {
  76. if (keyCode == KeyEvent.KEYCODE_BACK) {
  77. FinishActivity();
  78. }
  79. return false;
  80. }
  81. }

自定义悬浮按钮: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. jquery mobile将页面内容当成弹框进行显示

    注:必须使用相对应版本的jquery mobile css.不然无法正常显示 <div data-role="page" id="pageone"> ...

  2. 深入mysql慢查询设置的详解

    set long_query_time=1; #设置慢查询时间为1 秒; set global slow_query_log=on; #开启慢查询日志; show global status like ...

  3. 手机端禁止iPhone字体放大

    /*禁止iphone字体放大 */ html { -webkit-text-size-adjust: none; }

  4. JS表单验证类HTML代码实例

    以前用的比较多的一个JS表单验证类,对于个人来说已经够用了,有兴趣的可以在此基础上扩展成ajax版本.本表单验证类囊括了密码验证.英文4~10个 字符验证. 中文非空验证.大于10小于100的数字.浮 ...

  5. symfony框架在中国移动cmwap网络下访问的问题

    最近用symfony框架给手机app做后台,发现在中国移动cmwap网络下会出现问题,所有请求都路由到根路径了 左图为原始$_SERVER对象,右图为$request->server对象由于re ...

  6. Delphi窗体创建释放过程及单元文件小结(转)

    Delphi窗体创建释放过程及单元文件小结 Delphi中的窗体,有模式窗体与非模式窗体两种.两种窗体的调用方式不同,模式窗体使用ShowModal显示,非模式窗体使用Show显示.当显示模式窗体的时 ...

  7. Centos+nginx+uwsgi+Python多站点环境搭建

    前言 新公司的第一个项目,服务器端打算用python作为restful api.所以需要在Centos上搭建nginx+fastcgi+python的开发环境,但后面网上很多言论都说uwsgi比fas ...

  8. 文本框Edit

    支持换行就要把 Multiline 设置为TRUE Edit窗口是用来接收用户输入最常用的一个控件.创建一个输入窗口可以使用成员函数: BOOL CEdit::Create( LPCTSTR lpsz ...

  9. d017: 打印某年某月有几天

    内容: 打印某年某月有几天 输入说明: 一行两个整数,前面是年份 后面是月份 输出说明: 一个整数 输入样例:   2009 6 输出样例 : 30 #include <stdio.h> ...

  10. C#执行javascript代码

    最近在做网站自动登陆小工具,遇到技术屏障.密码在submit时会使用js进行加密.这时我需要模拟这个加密过程,想到使用C#执行javascript代码. 对于执行javascript代码,纯代码方式使 ...