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;
				}

			}
		};

}
图片资源我会随源代码一起给你们,下载吧。直接用到项目,简单高效哈哈


源代码下载地址




posted @ 2017-08-20 12:57  yangykaifa  阅读(1194)  评论(0编辑  收藏  举报