仿微信长按弹出popwindow效果
2016-05-30 16:11
302 查看
转载自
http://my.oschina.net/moziqi/blog/497429
主要关注用PopWindow实现:
利用PopupWindow实现时,首先它弹出来的时候我们就要设置当前界面的透明度,然后监听PopupWindow的dismiss消失事件,再PopupWindow消失时候再恢复当前界面的透明度,这样就是实现了弹出时候背景变暗,消失时候背景恢复了:
xml布局文件为:
代码实现方法为:
效果图如下:
http://my.oschina.net/moziqi/blog/497429
主要关注用PopWindow实现:
利用PopupWindow实现时,首先它弹出来的时候我们就要设置当前界面的透明度,然后监听PopupWindow的dismiss消失事件,再PopupWindow消失时候再恢复当前界面的透明度,这样就是实现了弹出时候背景变暗,消失时候背景恢复了:
xml布局文件为:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="20dp" android:paddingLeft="10dp" android:text="请选择" android:textColor="#ff2525" android:textSize="20sp" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:background="#969696" /> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:gravity="left|center_vertical" android:padding="10dp" android:text="第一个功能" android:textColor="#2a2a2a" android:textSize="15sp" android:background="#00000000"/> <View android:layout_width="match_parent" android:layout_height="1px" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:background="#a6a6a6" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:gravity="left|center_vertical" android:padding="10dp" android:text="第二个功能" android:textColor="#2a2a2a" android:textSize="15sp" android:background="#00000000" /> <View android:layout_width="match_parent" android:layout_height="1px" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:background="#a6a6a6" /> <Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:background="#00000000" android:gravity="left|center_vertical" android:padding="10dp" android:text="第三个功能" android:textColor="#2a2a2a" android:textSize="15sp" /> </LinearLayout> </LinearLayout>
代码实现方法为:
import android.annotation.SuppressLint; import android.app.ActionBar.LayoutParams; import android.app.Activity; import android.graphics.Point; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.view.Display; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.WindowManager; import android.widget.Button; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.PopupWindow.OnDismissListener; public class FirstActivity extends ActionBarActivity{ private But bcd0 ton button; private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first); button = (Button) findViewById(R.id.button); // listView = (ListView) findViewById(R.id.list_view); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { popWindow(); } }); } @SuppressLint("NewApi") private void popWindow(){ LayoutInflater inflater = LayoutInflater.from(this);//获取一个填充器 View view = inflater.inflate(R.layout.pop_window, null);//填充我们自定义的布局 Display display = getWindowManager().getDefaultDisplay();//得到当前屏幕的显示器对象 Point size = new Point();//创建一个Point点对象用来接收屏幕尺寸信息 display.getSize(size);//Point点对象接收当前设备屏幕尺寸信息 int width = size.x;//从Point点对象中获取屏幕的宽度(单位像素) int height = size.y;//从Point点对象中获取屏幕的高度(单位像素) Log.v("zxy", "width="+width+",height="+height);//width=480,height=854可知手机的像素是480x854的 //创建一个PopupWindow对象,第二个参数是设置宽度的,用刚刚获取到的屏幕宽度乘以2/3,取该屏幕的2/3宽度,从而在任何设备中都可以适配,高度则包裹内容即可,最后一个参数是设置得到焦点 PopupWindow popWindow = new PopupWindow(view, 2*width/3, LayoutParams.WRAP_CONTENT, true); popWindow.setBackgroundDrawable(new BitmapDrawable());//设置PopupWindow的背景为一个空的Drawable对象,如果不设置这个,那么PopupWindow弹出后就无法退出了 popWindow.setOutsideTouchable(true);//设置是否点击PopupWindow外退出PopupWindow WindowManager.LayoutParams params = getWindow().getAttributes();//创建当前界面的一个参数对象 params.alpha = 0.8f;//设置参数的透明度为0.8,透明度取值为0~1,1为完全不透明,0为完全透明,因为android中默认的屏幕颜色都是纯黑色的,所以如果设置为1,那么背景将都是黑色,设置为0,背景显示我们的当前界面 getWindow().setAttributes(params);//把该参数对象设置进当前界面中 popWindow.setOnDismissListener(new OnDismissListener() {//设置PopupWindow退出监听器 @Override public void onDismiss() {//如果PopupWindow消失了,即退出了,那么触发该事件,然后把当前界面的透明度设置为不透明 WindowManager.LayoutParams params = getWindow().getAttributes(); params.alpha = 1.0f;//设置为不透明,即恢复原来的界面 getWindow().setAttributes(params); } }); //第一个参数为父View对象,即PopupWindow所在的父控件对象,第二个参数为它的重心,后面两个分别为x轴和y轴的偏移量 popWindow.showAtLocation(inflater.inflate(R.layout.first, null), Gravity.CENTER, 0, 0); } }
效果图如下:
相关文章推荐
- 微信公众号开发——入门
- php 微信模拟登陆给用户发送消息(文字,图片,图文)
- C# 微信支付教程系列之扫码支付
- 微信上传永久素材接口开发(二)
- 微信上传永久素材接口开发(一)
- iOS中制作微信摇一摇的图框雏形
- 【原创分享·微信支付】 C# MVC 微信支付教程系列之公众号支付
- 微信开发提交服务器配置时提示URL超时
- ios下微信标题修改
- 微信支付之JSAPI公众号支付
- 微信三级分销系统
- 搞定支付宝和微信支付的各种填坑
- 微信坑爹锦集
- 微信支付遇到的超级蛋疼的事
- 禁止手机浏览器、微信浏览器的上下滑动露出黑色块问题
- Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果
- 10分钟搞定支付宝和微信支付 的 各种填坑
- 大把大把的钱砸进微信公共号,未见流量却成企业成本黑洞
- 微信公众号——扫码支付理解
- 微信三级分销合法吗