项目中,我须要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 仿微信弹出效果的更多相关文章

  1. PopupWindow 防微信弹出右 侧窗体(继承PopupWindow )

    1, pop自定义 public class SelectPicPopupWindow extends PopupWindow { private Button btn_take_photo, btn ...

  2. Android 仿 新闻阅读器 菜单弹出效果(附源码DEMO)

    这一系列博文都是:(android高仿系列)今日头条 --新闻阅读器 (一) 开发中碰到问题之后实现的,觉得可能有的开发者用的到或则希望独立成一个小功能DEMO,所以就放出来这么一个DEMO. 原本觉 ...

  3. Android listview 制作表格样式+由下往上动画弹出效果实现

    效果是这样的:点击按下弹出表格的按钮,会由下往上弹出右边的列表,按下返回按钮就由上往下退出界面. 布局文件: activity_main.xml <RelativeLayout xmlns:an ...

  4. 仿简书、淘宝等等App的View弹出效果

    昨天用简书App的时候觉得这个View的弹出效果特别好,而且非常平滑,所以我就尝试写了一个,和简书App上的效果基本一致了: 下面开始讲解: 1.首先我们要知道这个页面有几个View?这个页面其实有四 ...

  5. Android之仿微信图片选择器

    先上效果图.第一张图显示的是“相机”文件夹中的所有图片:通过点击多张图片可以到第二张图所示的效果(被选择的图片会变暗,同时选择按钮变亮):点击最下面的那一栏可以到第三张图所示的效果(显示手机中所有包含 ...

  6. 【Android】创建Popwindow弹出菜单的两种方式

    方法一的Activity package com.app.test02; import android.app.Activity; import android.os.Bundle; import a ...

  7. Android于popWindow写弹出菜单

    1.什么是popWindow? popWindow这是对话的方式!文字解说android的方式来使用对话框,这就是所谓的popWindow. 2.popWindow特征 Android的对话框有两种: ...

  8. Android EditText默认不弹出输入法,以及获取光标,修改输入法Enter键的方法

    一.Android EditText默认不弹出输入法的办法:1. 在AndroidManifest.xml中将需要默认隐藏键盘的Activity中添加属性即可(常用此方法) android:windo ...

  9. [转]Android 超高仿微信图片选择器 图片该这么加载

    快速加载本地图片缩略图的方法: 原文地址:Android 超高仿微信图片选择器 图片该这么加载 其示例代码下载: 仿微信图片选择器 ImageLoader

随机推荐

  1. logback实践笔记

    前言 ​ 每次看公司配置好的logback文件的时候,都不知道什么意思.导致有的时候,一些项目发到测试环境的时候,有的项目没有打印日志,自己都不知道哪里有问题.所以自己新建一个springboot项目 ...

  2. 在控制台下玩玩dotnet core内置原生的DI

    转载请注明出处:http://www.cnblogs.com/zhiyong-ITNote/ 在基于dotnet core的web开发中,我们会经常用到DI,那么如果单单使用dotnet core自身 ...

  3. 翻转长方形 (不知名oj中一道个人私题)--单调栈维护最大子矩形

    怎么分析这道题呢? 首先 ,我们注意到一点: 不管怎么操作,任意一个2*2方格中的 "#"个数的奇偶性是不变的. 所以,如果一个2*2方格中有奇数个"#",这个 ...

  4. 在Qt中调用Mupdf库进行pdf显示

    2018.5.10 更新内存对齐说明 感谢知乎网友@孤独子狮 指出QImage处需要考虑内存对齐的问题.因为本人缺乏跨平台.图形库开发经验,所以在调试成功后就没有深入探究. 主要修改了QImage的构 ...

  5. Linux服务部署--Java(二)

    八.Maven安装配置 1. 下载 wget http://mirrors.cnnic.cn/apache/maven/maven-3/3.3.9/binaries/apache-maven-3.3. ...

  6. Ubuntu环境中的Android源代码下载

    跟随“老罗的Android之旅”学习Android系统,首先得学会创建能用于编译Android源代码的环境. 文章参考:http://0xcc0xcd.com/p/books/978-7-121-18 ...

  7. 潭州课堂25班:Ph201805201 django框架 第十课 GET,POST 请求 文件上传,HttpResponse,cookie (课堂笔记)

    在项目中新建个APP, 在主目录中的配置文件中进行 APP 注册 在主目录中的 urls 文件中进行路径分配 新建 urrls 文件 进行分路由配置 创建模板文件 html 文件 写个 form 表单 ...

  8. 基于Systick系统时钟延时的LED闪烁灯

    1.回顾我们的51 单片机编程,当我们需要做系统延迟的时候,最常采用的一种方式就是使用for 循环的空语句等待来实现. 当然,在STM32 里面也可以这么实现.但是在STM32 的Cortex 内核里 ...

  9. BZOJ5101 : [POI2018]Powód

    求出Kruskal重构树,那么重构树上每个点的取值范围是定的. 考虑树形DP,则对于一个点,要么所有点水位相同,要么还未发生合并. 故$dp[x]=up[x]-down[x]+1+dp[l[x]]\t ...

  10. [P4995]跳跳!(贪心)

    这应该是我做过的最简单的洛谷月赛了 题意 给你n个高度,你的初始高度是0,现在要求你遍历每一个高度,每一次遍历耗费(hi−hj)2 的值 现在要你求耗费值最大 思路 真的是水…… 排序一下,求一下就好 ...