安卓PopupWindow的详细用法、位置、动画效果等

安卓PopupWindow是一个方便并且使用很常见的弹窗,下面详细介绍下PopupWindow的使用。

先附上效果图:


//图截得不太好,大家凑合这用,重点是它的用法。

//关于popupWindow的实现关键代码如下:

package com.lyk.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lyk.listview2.R;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity 
{
	private ListView listView;
	private SimpleAdapter adapter;
	private String[] from = {"text"};
	private int[] to = {R.id.text};
	private Map<String,Object> map;
	
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		listView = (ListView)findViewById(R.id.listView);
		adapter = new SimpleAdapter(this,getData(),R.layout.simplaadapter_listview,from,to);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new onItemClickListeners());
		
	}
	/**
	 * 数据源
	 * @return
	 */
	private List<? extends Map<String, ?>> getData(){	
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		
		for(int i = 0;i<7;i++){
			map = new HashMap<String, Object>();
			map.put("text","item"+i);
			list.add(map);
		}
		return list;
	}
	/**
	 * 条目事件
	 * @author Administrator
	 *
	 */
	final class onItemClickListeners implements OnItemClickListener{

		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			System.out.println("id"+id);
			System.out.println("position"+position);
			System.out.println("view"+view.toString());//relative
			System.out.println("parent"+parent.toString());//listview
			
			System.out.println(parent.getChildAt(0).toString());
			
			initPopup();
		  	popup.showAsDropDown(view,0,5);//相对某个控件的位置(正左下方),偏移可选
			
		}
		
	}
	
	private View parent;//popup相对view
	private PopupWindow popup;
	private View contentView;
	public void initPopup(){
		
		//初始动画
//		animation = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.bottom);
		
		//从指定的xml添加一个view
		contentView = getLayoutInflater().inflate(R.layout.popup_window,null);
		popup = new PopupWindow(contentView,ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		popup.setFocusable(true);//设置焦点
	  	popup.setBackgroundDrawable(new BitmapDrawable());//设置背景位图
	  	popup.setAnimationStyle(R.style.animation);//设置动画
	  	
	  	parent = this.findViewById(R.id.main);
//	  	popup.showAtLocation(parent,Gravity.BOTTOM,0,0);//在父容器的位置
	}

	public boolean onCreateOptionsMenu(Menu menu) 
	{
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
//要实现popupWindow的动画效果,还需下面的两个平移动画(底部):

//进入动画:

<?xml version="1.0" encoding="utf-8"?>
<set 
    xmlns:android="http://schemas.android.com/apk/res/android"     
    android:shareInterpolator="false" 
    >
	<!-- 平移动画 -->
	<translate             
	    android:fromYDelta="100%p"        
	    android:toYDelta="0" 
	    android:duration="600"
	    />
	<!-- 透明度改变动画 -->
	<alpha        
	    android:fromAlpha="0"        
	    android:toAlpha="1.0" 
	    android:duration="600"
	    />

</set>
//隐藏PopupWindow动画:

<?xml version="1.0" encoding="utf-8"?>
<set 
    xmlns:android="http://schemas.android.com/apk/res/android"     
    android:shareInterpolator="false" 
    >
	<!-- 平移动画 -->
	<translate             
	    android:fromYDelta="0"        
	    android:toYDelta="100%p" 
	    android:duration="1000"
	    />
	<!-- 透明度改变动画 -->
	<alpha        
	    android:fromAlpha="1.0"        
	    android:toAlpha="0" 
	    android:duration="1000"
	    />

</set>
//实现PopupWindow窗口就是这么简单,大家值得注意和研究的是PopupWindow的显示位置

他有两种API显示:

1、popup.showAsDropDown(view,0,5);//相对某个控件的位置(正左下方),偏移可选

2、popup.showAtLocation(parent,Gravity.BOTTOM,0,0);//在父容器的位置,在底部(偏移可选)



阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页