Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android PopupWindow 仿微信彈出效果

Android PopupWindow 仿微信彈出效果

編輯:關於Android編程

項目中,我需要PopupWindow的時候特別多,這個東西也特別的好使,所以我今天給大家寫一款PopupWindow 仿微信彈出效果,這樣大家直接拿到項目裡就可以用了!首先讓我們先看效果:

\

那麼我首先先看下布局代碼非常簡單:如下

 


下面我們來看核心的代碼,如下

 

 

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

我們在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;
				}

			}
		};

}
圖片資源我會隨源碼一起給你們,下載吧,直接用到項目,簡單高效哈哈

 

 

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved