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
随机推荐
- 真正“搞”懂http协议01—背景故事
去年读了<图解HTTP>.<图解TCP/IP>以及<图解网络硬件>但是读了之后并没有什么深刻的印象,只是有了一层模糊的脉络,刚好最近又接触了一些有关http的相关内 ...
- Misunderstood-Missing-逆向DP
Misunderstood … Missing 记忆深刻......打铁没做出来的题 题意 : 打怪,有 A 的攻击力,有 D 的成长,初始均为 0,有 n 轮. 同时有三个数组 a[1:n],b[1 ...
- Kafka、RabbitMQ、RocketMQ等消息中间件的对比 —— 消息发送性能和区别
https://blog.csdn.net/yunfeng482/article/details/72856762
- Windows下更改MySQL 数据库文件存放位置
更改默认的mysql数据库目录 将 C:\Documents and Settings\All Users\Application Data\MySQL\MySQL Server 5.1\data 改 ...
- XamarinSQLite教程下载安装SQLite/SQL Server Compact Toolbox
XamarinSQLite教程下载安装SQLite/SQL Server Compact Toolbox SQLite/SQL Server Compact Toolbox是一个Visual Stud ...
- elementUI的table组件实现setCurrentRow的滚动条定位效果
在github上咨询了,直接给了代码: https://jsfiddle.net/tk37c5cb/14/
- [R] R dataframe 中对列使用sort或者order的注意
存在这样的需求: 针对每列的值, 对列进行排序. 这样处理过数据后, 在excel中对数据作图时, 使数据呈现有序. R中sort数据的时候, 如果数据中存在字符串, R会将数据转化为characte ...
- wget用法汇总
wget是在Linux下开发的开放源代码的软件,作者是Hrvoje Niksic,后来被移植到包括Windows在内的各个平台上.它有以下功能和特点: (1)支持断点下传功能:这一点,也是网络蚂蚁和F ...
- redis安装,第一天
1.直接官网下载 2.进入redis安装目录 : cd /redis-4.0.11 make install 3.启动:redis-server /myredis/redis.conf //复制 ...
- c# 后台绑定treeview 单个tab
<wijmo:C1TreeView ID="C1TreeView1" runat="server" ShowCheckBoxes="true&q ...