Android PopupWindow 仿微信弹出效果,androidpopupwindow
时间: 2019-02-01 来源: admin

Android PopupWindow 仿微信弹出效果,androidpopupwindow

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


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

<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弹出窗体后,为何不可以点击其他控件

因为PopupWindow获得了焦点,其他得不到焦点,所以无法点击,把setFocusable设为false就行了。
 
android PopupWindow 上下菜单实现 按Menu 实现顶部+底部菜单同时出现,按back同时消失

重写onKeyDown方法
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_MENU){
Log.i("aaa", "keyCode="+keyCode);
PopupWindow .showXXXXXXX();
}
if(keyCode==KeyEvent.KEYCODE_BACK){
Log.i("aaa", "keyCode="+keyCode);
PopupWindow.dismiss();
return true;
}
return super.onKeyDown(keyCode, event);

}
 

http://www.bkjia.com/Androidjc/867471.htmlwww.bkjia.comtruehttp://www.bkjia.com/Androidjc/867471.htmlTechArticleAndroid PopupWindow 仿微信弹出效果,androidpopupwindow 项目中,我需要PopupWindow的时候特别多,这个东西也特别的好使,所以我今天给大家写一款...

本文源自: 环亚国际



上一篇:Android ListView那些事
下一篇:没有了