码迷,mamicode.com
首页 > 移动开发 > 详细

我的手机管家(19) 应用管理 单独介绍一下PopupWindow

时间:2016-07-05 14:13:37      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

我的手机管家(19) 应用管理  单独介绍一下PopupWindow

在前一节中使用点击ListView 出现一个弹出窗口, 用来显示我们的需求。

PopupWindow: A popup window that can be used to display an arbitrary view. The popup window is a floating container that appears on top of the current activity.

    PopupWindow 是一个弹出窗体, 可以显示任意的视图, 他是一个浮动的容器, 显示在当前activity的上方。

注意要给PopupWindow设置一个背景,否则,会出现点击back键和点击PopupWindow区域外, PopupWindow不会消失, 

    设置背景:mPopupWindow.setBackgroundDrawable(new ColorDrawable());

(1)按钮点击触发 PopupWindow的显示, 

  mPopupWindow.showAsDropDown(pView,60,-pView.getHeight());  显示PopupWindow的位置
  设置popView的弹出效果动画,

(2) 当点击PopupWindow区域以外的地方, PopupWindow消失

(3)点击PopupWindow视图的选项,执行相应的操作。

 

package com.example.testpopmenu;
 

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
    PopupWindow mPopupWindow;
    View popupView;
    AnimationSet animationset;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    /**
     * 
     * @param v
     */
    public void Click(View v){
        showPopupWindow(v);
    }
    
    @Override
    public void onClick(View v) { 
         if(mPopupWindow!= null){
            mPopupWindow.dismiss();//消失
        } 
        switch (v.getId()) {
        case R.id.tv_lanuch://启动
            Toast.makeText(this, "启动", Toast.LENGTH_LONG).show();
            break;
        case R.id.tv_uninstall://卸载
            Toast.makeText(this, "系统应用,需要root权限才可以卸载", Toast.LENGTH_LONG).show();
            break;
        case R.id.tv_share://分享
            Toast.makeText(this, "分享", Toast.LENGTH_LONG).show();
           break;
        default:
            break;
        }
    }
    
    /**
     *弹出窗口 
     */
    protected void showPopupWindow(View pView){
        if(mPopupWindow==null){
            popupView = View.inflate(this, R.layout.popup_adapter, null);
            mPopupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,true);
            //设定一个背景,这样的话点击返回键可用消失
            mPopupWindow.setBackgroundDrawable(new ColorDrawable());
            TextView tvStart = (TextView) popupView.findViewById(R.id.tv_lanuch);
            TextView tvUninstall = (TextView) popupView.findViewById(R.id.tv_uninstall);    
            TextView tvShare=(TextView) popupView.findViewById(R.id.tv_uninstall);
            
            tvStart.setOnClickListener(this);
            tvUninstall.setOnClickListener(this);
            tvShare.setOnClickListener(this);
            
            //缩放动画
            ScaleAnimation a= new ScaleAnimation(0, 1,0,1,
                    Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f
                    );
            a.setDuration(200);
            
            //渐变动画
            AlphaAnimation alpha = new AlphaAnimation(0, 1);
            alpha.setDuration(200);
            animationset = new AnimationSet(false);
            animationset.addAnimation(a);
            animationset.addAnimation(alpha);
            
        }
        mPopupWindow.showAsDropDown(pView,60,-pView.getHeight());
        popupView.startAnimation(animationset);
    }
 

}

 

 

我的手机管家(19) 应用管理 单独介绍一下PopupWindow

标签:

原文地址:http://www.cnblogs.com/chengbao/p/5643304.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!