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
随机推荐
- vsftpd中配置文件详解
在vsftp服务器中,配置文件/etc/vsftpd/vsftpd.conf文件是配置的核心内容,其具体的配置信息详细情况如下: 1.默认配置: 1>允许匿名用户和本地用户登陆. anonymo ...
- c++简单学习
在c++中我们很容易遇到字符串的分割处理问题,这种问题通常比较容易,但由于我比较菜,花费了一定时间去思考一个和字符串相关的题,该题的大概思路是利用取模运算后,将得到的单个字符进行分析,主要考察到了字符 ...
- 想造轮子的时候,ctrl+f一下
Chardet,字符编码探测器,可以自动检测文本.网页.xml的编码. colorama,主要用来给文本添加各种颜色,并且非常简单易用. Prettytable,主要用于在终端或浏览器端构建格式化的输 ...
- BZOJ.5093.[Lydsy1711月赛]图的价值(NTT 斯特林数)
题目链接 对于单独一个点,我们枚举它的度数(有多少条边)来计算它的贡献:\[\sum_{i=0}^{n-1}i^kC_{n-1}^i2^{\frac{(n-2)(n-1)}{2}}\] 每个点是一样的 ...
- 1418 抱歉 ACM 欧拉公式
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1418 思路:一看题目,明显是要求我们找规律,于是我就在草稿纸上画了很多个图像,还是没有找到规律,于是我就在 ...
- php 公共方法Util
总结了一个公共类方法类: class Util extends ArrayHelper { /** * 判断是否为空数组 * @param mixed $arr * @return boolean * ...
- yii2 basic版 MVC 部分
一.model 1.什么是模型 模型是 MVC 模式中的一部分,是表现业务数据.规则和逻辑的对象 可通过继承 yii\base\Model 或它的子类(activeRecord)来定义模型类,基类 y ...
- button标题左对齐
Button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;//左对齐(UIControlContentHo ...
- flask 跨域问题
在Flask开发RESTful后端时,前端请求会遇到跨域的问题.下面是解决方法.Python版本:3.5.1 下载flask_cors包 pip install flask-cors使用flask_c ...
- HTML5 学习01——浏览器问题、新元素
Internet Explorer 浏览器问题 问题:Internet Explorer 8 及更早 IE 版本的浏览器不支持HTML5的方式. <!--[if lt IE 9]> < ...