PopupWindow 的实现

PopupWindow的实现方式

 

Android的对话框常用的有两种:PopupWindowAlertDialogPopupWindow顾名思义为弹出菜单,不同于AlertDialog对话框,PopupWindow弹出的位置可以很多变化,按照有无偏移分,可以分为无偏移和偏移两种;PopupWindow是不阻塞的对话框,AlertDialog是阻塞对话框。按照参照类型不同又可以分为两种:相对某个控件(Anchor锚)的位置和父容器内部的相对位置。具体如下:

 

函数简介 

showAsDropDown(View anchor)

相对某个控件的位置(正左下方),无偏移

showAsDropDown(View anchor, int xoff, int yoff)

相对某个控件的位置,有偏移(正数表示下方右边,负数表示(上方左边))

showAtLocation(View parent, int gravity, int x, int y)

父容器容器相对位置,例如正中央Gravity.CENTER,下方Gravity.BOTTOM

 

 

注:PopupWindow弹出时,Activity不会调用onPause(),即它不会阻塞调用它的Activity

 

主程序:

package com.example.painttest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

public class PopupDemo extends Activity {

	private Button buttonDefault;
	private Button buttonThree;
	private Button buttonLocation;
	private Button close;
	private PopupWindow pop;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//获得控件的引用
		buttonDefault = (Button)findViewById(R.id.buttonDefault);
		buttonThree = (Button)findViewById(R.id.buttonThree);
		buttonLocation = (Button)findViewById(R.id.buttonLocation);
		//在onCreate中队PopupWindow进行初始化
		initPopupWindow();
		//给按键设置监听器
		buttonDefault.setOnClickListener(new ButtonListener());
		buttonThree.setOnClickListener(new ButtonListener());
		buttonLocation.setOnClickListener(new ButtonListener());
	}
	//按键监听器类
	class ButtonListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.buttonDefault:			//使用带一个参数的showAsDropDown()方法,把PopupWindow放在触发它的窗体右下角
				if (pop.isShowing()) {
					pop.dismiss();
				}else {
					pop.showAsDropDown(v);
				}
				break;
			case R.id.buttonThree:			//使用带三个参数的showAsDropDown()方法,把PopupWindow放置在触发它的窗体的右下角相对移动X,Y的位置处
				if (pop.isShowing()) {
					pop.dismiss();
				}else {
					pop.showAsDropDown(v, 0, -200);
				}
				break;
			case R.id.close:			//PopupWindow上的按键对应的事件处理。
				pop.dismiss();
				break;
			default:
				if (pop.isShowing()) {
					pop.dismiss();
				} else {
					pop.showAtLocation(v, Gravity.CENTER_VERTICAL, 100, 100);
				}
				break;
			}
		}
		
	}
	
	public void initPopupWindow() {
		View view = this.getLayoutInflater().inflate(R.layout.popupwindow, null);		//通过Infalter使用布局文件得到PopupWindow视图
		pop = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);//设置PopupWindow的布局
											
		pop.setOutsideTouchable(true);				//设置PopupWindow外是否可以触摸
		
		view.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				pop.dismiss();
			}
		});
		try {
			close = (Button)view.findViewById(R.id.close);
			close.setOnClickListener(new ButtonListener());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		
	}

	
}

主界面布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/textHint"
        android:textSize="20sp" />

    <Button
        android:id="@+id/buttonDefault"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView"
        android:layout_marginTop="24dp"
        android:text="@string/buttonDefault" />

    <Button
        android:id="@+id/buttonThree"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/buttonDefault"
        android:layout_marginTop="24dp"
        android:text="@string/buttonThree" />

    <Button
        android:id="@+id/buttonLocation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="@string/buttonLoaction" />

</RelativeLayout>

PopupWindow布局窗口:

<?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="fill_parent"
    android:background="#d0d0d0"
    android:orientation="vertical" >




    <Button
        android:id="@+id/close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/colse" />

</LinearLayout>



你可能感兴趣的:(PopupWindow 的实现)