Android PopupWindow 仿微信弹出效果
项目中,我须要PopupWindow的时候特别多,这个东西也特别的好使,所以我今天给大家写一款PopupWindow 仿微信弹出效果。这样大家直接拿到项目里就能够用了!
首先让我们先看效果:
那么我首先先看下布局代码很easy:例如以下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pop_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg_pulish"
android:gravity="center"
android:orientation="vertical" > <Button
android:id="@+id/btn_hand"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="20dip"
android:background="@drawable/bg_publish_product_selector"
android:text="1"
android:textStyle="bold" /> <Button
android:id="@+id/btn_library"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="20dip"
android:background="@drawable/bg_publish_product_selector"
android:text="2"
android:textStyle="bold" /> <Button
android:id="@+id/btn_two_code"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="20dip"
android:background="@drawable/bg_publish_product_selector"
android:text="3"
android:textStyle="bold" /> <Button
android:id="@+id/btn_cancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="30dip"
android:layout_marginBottom="30dp"
android:background="@drawable/bg_publish_product_selector"
android:text="取消"
android:textStyle="bold" /> </LinearLayout>
以下我们来看核心的代码。例如以下
package com.example.showpopupwindow; import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow; public class PublishSelectPicPopupWindow extends PopupWindow { private Button btnHand;
private Button btnLibrary;
private Button btnTwocode;
private Button btnCancel;
private View mMenuView; @SuppressWarnings("deprecation")
public PublishSelectPicPopupWindow(Activity context,OnClickListener itemsOnClick) {
super(context);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mMenuView = inflater.inflate(R.layout.publish_dialog, null);
btnHand = (Button) mMenuView.findViewById(R.id.btn_hand);
btnLibrary = (Button) mMenuView.findViewById(R.id.btn_library);
btnTwocode=(Button) mMenuView.findViewById(R.id.btn_two_code);
btnCancel = (Button) mMenuView.findViewById(R.id.btn_cancel);
btnCancel.setOnClickListener(new OnClickListener() { public void onClick(View v) {
dismiss();
}
});
//设置button监听
btnHand.setOnClickListener(itemsOnClick);
btnLibrary.setOnClickListener(itemsOnClick);
btnTwocode.setOnClickListener(itemsOnClick);
btnCancel.setOnClickListener(itemsOnClick);
//设置SelectPicPopupWindow的View
this.setContentView(mMenuView);
//设置SelectPicPopupWindow弹出窗口的宽
this.setWidth(LayoutParams.FILL_PARENT);
//设置SelectPicPopupWindow弹出窗口的高
this.setHeight(LayoutParams.WRAP_CONTENT);
//设置SelectPicPopupWindow弹出窗口可点击
this.setFocusable(true);
//设置SelectPicPopupWindow弹出窗口动画效果
this.setAnimationStyle(R.style.AnimBottom);
//实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(0xb0000000);
//设置SelectPicPopupWindow弹出窗口的背景
this.setBackgroundDrawable(dw);
//mMenuView加入OnTouchListener监听推断获取触屏位置假设在选择框外面则销毁弹出框
mMenuView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) { int height = mMenuView.findViewById(R.id.pop_layout).getTop();
int y=(int) event.getY();
if(event.getAction()==MotionEvent.ACTION_UP){
if(y<height){
dismiss();
}
}
return true;
}
}); } }
我们在MainActivity调用我们写好的代码 例如以下:
package com.example.showpopupwindow; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends Activity {
private PublishSelectPicPopupWindow menuWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button=(Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// 实例化SelectPicPopupWindow
menuWindow = new PublishSelectPicPopupWindow(MainActivity.this,itemsOnClick);
// 显示窗体
menuWindow.showAtLocation(MainActivity.this.findViewById(R.id.button1),
Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
}
});
}
// 为弹出窗体实现监听类
private OnClickListener itemsOnClick = new OnClickListener() { public void onClick(View v) {
menuWindow.dismiss();
switch (v.getId()) {
case R.id.btn_hand:
Toast.makeText(MainActivity.this, "1", 2).show();
break;
case R.id.btn_two_code:
Toast.makeText(MainActivity.this, "2", 2).show();
break;
case R.id.btn_library:
Toast.makeText(MainActivity.this, "3", 2).show();
break;
default:
break;
} }
}; }
图片资源我会随源代码一起给你们,下载吧。直接用到项目,简单高效哈哈
Android PopupWindow 仿微信弹出效果的更多相关文章
- PopupWindow 防微信弹出右 侧窗体(继承PopupWindow )
1, pop自定义 public class SelectPicPopupWindow extends PopupWindow { private Button btn_take_photo, btn ...
- Android 仿 新闻阅读器 菜单弹出效果(附源码DEMO)
这一系列博文都是:(android高仿系列)今日头条 --新闻阅读器 (一) 开发中碰到问题之后实现的,觉得可能有的开发者用的到或则希望独立成一个小功能DEMO,所以就放出来这么一个DEMO. 原本觉 ...
- Android listview 制作表格样式+由下往上动画弹出效果实现
效果是这样的:点击按下弹出表格的按钮,会由下往上弹出右边的列表,按下返回按钮就由上往下退出界面. 布局文件: activity_main.xml <RelativeLayout xmlns:an ...
- 仿简书、淘宝等等App的View弹出效果
昨天用简书App的时候觉得这个View的弹出效果特别好,而且非常平滑,所以我就尝试写了一个,和简书App上的效果基本一致了: 下面开始讲解: 1.首先我们要知道这个页面有几个View?这个页面其实有四 ...
- Android之仿微信图片选择器
先上效果图.第一张图显示的是“相机”文件夹中的所有图片:通过点击多张图片可以到第二张图所示的效果(被选择的图片会变暗,同时选择按钮变亮):点击最下面的那一栏可以到第三张图所示的效果(显示手机中所有包含 ...
- 【Android】创建Popwindow弹出菜单的两种方式
方法一的Activity package com.app.test02; import android.app.Activity; import android.os.Bundle; import a ...
- Android于popWindow写弹出菜单
1.什么是popWindow? popWindow这是对话的方式!文字解说android的方式来使用对话框,这就是所谓的popWindow. 2.popWindow特征 Android的对话框有两种: ...
- Android EditText默认不弹出输入法,以及获取光标,修改输入法Enter键的方法
一.Android EditText默认不弹出输入法的办法:1. 在AndroidManifest.xml中将需要默认隐藏键盘的Activity中添加属性即可(常用此方法) android:windo ...
- [转]Android 超高仿微信图片选择器 图片该这么加载
快速加载本地图片缩略图的方法: 原文地址:Android 超高仿微信图片选择器 图片该这么加载 其示例代码下载: 仿微信图片选择器 ImageLoader
随机推荐
- SQL 自定义存储过程报错
begin catch ), @ErrSeverity int SELECT @ErrMsg = ' SP Error: '+ERROR_MESSAGE(), @ErrSeverity = ERROR ...
- Python3基础-分数运算
Python3分数运算 fractions 模块可以被用来执行包含分数的数学运算. 案例 >>> from fractions import Fraction >>> ...
- Java笔记(十七) 异步任务执行服务
异步任务执行服务 一.基本原理和概念 一)基本接口 1)Runnable和Callable:表示要执行的异步任务. 2)Executor和ExecutorService:表示执行服务. 3)Futur ...
- 杭店 ACM 1864 最大报销额 01背包
![勾选C++才能过 题意: 先规定可以报销一定额度的发票,物品类型有A,B,C,三种.要求每张发票总额不得超过1000元,单项物品不得超过600.求报销的最大额 分析: 先找到合格的发票,然后再挑选 ...
- 通过Quartz 配置定时调度任务:使用cron表达式配置时间点
Cron官网入口 在后台经常需要一些定时处理的任务,比如微信相关应用所需的access_token,就要定时刷新,官方返回的有效性是7200s,也就是2小时,但是为了保险起见,除了在发现access_ ...
- YIi url美化
一.Yii Url美化,配置urlManager组件 'urlManager' => [ 'enablePrettyUrl' => true, // 开启URL美化,可以去掉 index. ...
- Android Studio 解决ADB检测不到手机导致无法连接的问题
ADB的全称是Android Debug Bridge,是用来管理模拟器和真机的通用调试工具. 开USB调试 方法:手机设置 - 开发人员选项 - USB调试 - 勾选(开发者调试被隐藏了,在关于手机 ...
- Idea-每次修改JS文件都需要重启Idea才能生效解决方法
最近开始使用Idea,有些地方的确比eclipse方便.但是我发现工程每次修改JS或者是JSP页面后,并没有生效,每次修改都需要重启一次Tomcat这样的确不方便.我想Idea肯定有设置的方法,不可能 ...
- django之模型层(model)--建表、查询、删除基础
要说一个项目最重要的部分是什么那铁定数据了,也就是数据库,这篇就开始带大家走进django关于模型层model的使用,model主要就是操纵数据库不使用sql语句的情况下完成数据库的增删改查.本篇仅带 ...
- golang 特殊知识点
golang 代码不需要分号; 但是又会自己在底层增加;号 ,所以 golang的{左花括号必须在代码的最后一行,而不能在新的一行; golang 代码组织里需要注意 vendor 和 interna ...