Popup Window 使用

参考了网上童鞋的作法,仔细研究了Popup Window 的用法,在此和大家分享下。



public class MainActivity extends Activity {
	private Button bt_order;
	private PopupWindow popupWindow;
	private LinearLayout layout;
	private ListView listView;
	private String title[] = { "全部", "我的微博", "周边", "推荐","智能排版","广场", "同学","密友" };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);
		bt_order = (Button) findViewById(R.id.tv_title);
		bt_order.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				showPopupWindowA();
//				showPopupWindowB();
			}
		});
	}

	/** 
     * 使用 showAsDropDown 方法显示 
     */  
    private void showPopupWindowB() {  
        layout = (LinearLayout) LayoutInflater.from(MainActivity.this).inflate(  
                R.layout.dialog, null);  
        listView = (ListView) layout.findViewById(R.id.lv_dialog);  
        listView.setAdapter(new ArrayAdapter<String>(MainActivity.this,  
                R.layout.text, R.id.tv_text, title));  
  
        popupWindow = new PopupWindow(MainActivity.this);  
        popupWindow.setBackgroundDrawable(new BitmapDrawable());  
        popupWindow  
        .setWidth(getResources().getDimensionPixelSize(  
                R.dimen.mark_popupwindow_width));  
        popupWindow.setHeight(getResources().getDimensionPixelSize(  
                R.dimen.mark_popupwindow_height));  
          
        popupWindow.setFocusable(true); // 设置PopupWindow可获得焦点  
        popupWindow.setTouchable(true); // 设置PopupWindow可触摸  
        popupWindow.setOutsideTouchable(false); // 设置非PopupWindow区域可触摸  
        popupWindow.setAnimationStyle(R.style.PopupAnimation);  
          
        popupWindow.setContentView(layout);  
          
//        popupWindow.showAsDropDown(bt_order);//popup window 位于锚点view左下角  
//        popupWindow.showAsDropDown(bt_order, 0, 0); //相对于锚点view bottom-left 的x,y offset
        
        popupWindow.showAsDropDown(bt_order, (getResources().getDimensionPixelSize(  
        		R.dimen.bt_width) - getResources().getDimensionPixelSize(  
        				R.dimen.mark_popupwindow_width))/2, 0);  
  
        listView.setOnItemClickListener(new OnItemClickListener() {  
  
            @Override  
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
                    long arg3) {  
                bt_order.setText(title[arg2]);  
                popupWindow.dismiss();  
                popupWindow = null;  
            }  
        });  
    }  
  
    /** 
     * 使用 showAtLocation 方法显示,copy网上某位博主的实现方式 
     */  
    public void showPopupWindowA() {  
        Rect frame = new Rect();  
        getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);  
        int state_heght = frame.top;// 状态栏的高度  
  
        int y = bt_order.getBottom()+state_heght ;  
        int x = getWindowManager().getDefaultDisplay().getWidth()/2 - getResources().getDimensionPixelSize(  
                R.dimen.mark_popupwindow_width)/2;  
          
        layout = (LinearLayout) LayoutInflater.from(MainActivity.this).inflate(  
                R.layout.dialog, null);  
        listView = (ListView) layout.findViewById(R.id.lv_dialog);  
        listView.setAdapter(new ArrayAdapter<String>(MainActivity.this,  
                R.layout.text, R.id.tv_text, title));  
  
        popupWindow = new PopupWindow(MainActivity.this);  
        popupWindow.setBackgroundDrawable(new BitmapDrawable()); //设置空drawable作背景 
        popupWindow.setWidth(getResources().getDimensionPixelSize(  
                R.dimen.mark_popupwindow_width));  
        popupWindow.setHeight(getResources().getDimensionPixelSize(  
                R.dimen.mark_popupwindow_height)); 
        
          
        popupWindow.setFocusable(true); // 设置PopupWindow可获得焦点  
        popupWindow.setTouchable(true); // 设置PopupWindow可触摸  
        popupWindow.setOutsideTouchable(false); // 设置非PopupWindow区域可触摸  
        popupWindow.setAnimationStyle(R.style.PopupAnimation);  
          
        popupWindow.setContentView(layout);  
//        popupWindow.showAtLocation(findViewById(R.id.main), Gravity.NO_GRAVITY, 0, 0);//需要指定Gravity,NO_GRAVITY等同于Left|Top
        		//x,y offset 是相对于Gravity的位置,当x,y为0 时,就完全是Gravity的位置
        		//showAtLocation()是在屏幕上的位置,showAsDropDown()是相对于锚点view的位置
        
        popupWindow.showAtLocation(findViewById(R.id.main), Gravity.NO_GRAVITY, x, y);
        
        listView.setOnItemClickListener(new OnItemClickListener() {  
  
            @Override  
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
                    long arg3) {  
                bt_order.setText(title[arg2]);  
                popupWindow.dismiss();  
                popupWindow = null;  
            }  
        });  
    }  

}


上面两种方法,一种是使用showAtLocation(), 这种方式popupWindow的位置参考整个屏幕, showAsDropDown(),这种方式popupWindow的位置参考指定的view


下面是pupupWindow 用到的动画文件
popupAnimation.xml  , R.style. popupAnimation

<?xml version="1.0" encoding="utf-8"?>
<resources>
    
     <style name="PopupAnimation" parent="android:Animation" mce_bogus="1">       
            <item name="android:windowEnterAnimation">@anim/popup_enter</item>  
        	<item name="android:windowExitAnimation">@anim/popup_exit</item>  
     </style>  

</resources>

R.anim.popup_enter

<?xml version="1.0" encoding="utf-8"?>      
<set xmlns:android="http://schemas.android.com/apk/res/android">  
     <scale android:fromXScale="0.6" android:toXScale="1.0"  
         android:fromYScale="0.6" android:toYScale="1.0" android:pivotX="50%"  
         android:pivotY="50%" android:duration="1000" />  
     <alpha android:interpolator="@android:anim/decelerate_interpolator"  
        android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="2000" />  
</set>  


R.anim.popup_exit
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
        <scale android:fromXScale="1.0" android:toXScale="0.5"
                android:fromYScale="1.0" android:toYScale="0.5" android:pivotX="50%"
                android:pivotY="50%" android:duration="500" />
        <alpha android:interpolator="@android:anim/accelerate_interpolator"
                android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="500" />
</set>  

在github上有哥们将popupWindow封装了一下,可自定义popupWindow中的内容。

GitHub 地址 https://github.com/lorensiuswlt/NewQuickAction





可根据需要自定义popupWindow,或者使用GitHub上那哥们写好的框架。


工程下载地址 http://download.csdn.net/detail/dtnqy/7112529



为popupWindow设置动画

在res/anim添加2个文件

menu_bottombar_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="250"
        android:fromYDelta="100.0%"
        android:toYDelta="0.0" />

</set>

menu_bottombar_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="250"
        android:fromYDelta="0.0"
        android:toYDelta="100%" />

</set>

在res/value/styles.xml添加一个sytle

    <style name="anim_menu_bottombar">
        <item name="android:windowEnterAnimation">@anim/menu_bottombar_in</item>
        <item name="android:windowExitAnimation">@anim/menu_bottombar_out</item>
    </style>

        pop.setAnimationStyle(R.style.anim_menu_bottombar);
        pop.showAtLocation(text, Gravity.BOTTOM, 0, 0);
setAnimationStyle()要在show之前。









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值