码迷,mamicode.com
首页 > 微信 > 详细

Android PopupWindow 仿微信弹出效果

时间:2014-08-20 21:09:32      阅读:614      评论:0      收藏:0      [点我收藏+]

标签:android   popupwindow   popupwindow仿微信弹出效果   

项目中,我需要PopupWindow的时候特别多,这个东西也特别的好使,所以我今天给大家写一款PopupWindow 仿微信弹出效果,这样大家直接拿到项目里就可以用了!首先让我们先看效果:

bubuko.com,布布扣

那么我首先先看下布局代码非常简单:如下

<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();
			}
		});
		//设置按钮监听
		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 仿微信弹出效果,布布扣,bubuko.com

Android PopupWindow 仿微信弹出效果

标签:android   popupwindow   popupwindow仿微信弹出效果   

原文地址:http://blog.csdn.net/xiaoyuan511/article/details/38711813

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!