请选择 进入手机版 | 继续访问电脑版
搜索
热搜: 活动 交友 discuz
查看: 963|回复: 0

[知识科普] 安卓弹出窗口案例

[复制链接]

0

主题

0

帖子

2

积分

新手上路

Rank: 1

积分
2
发表于 2018-7-27 14:22:07 | 显示全部楼层 |阅读模式
一.弹出窗体1.弹出窗体一定注意关闭2.弹出窗体要播放动画,必须有背景资源

  • 1.弹出窗体一定注意关闭2.弹出窗体要播放动画,必须有背景资源


二.源代码MainActivity

  • package com.itheima62.popupwindowdemo;

  • import android.app.Activity;
  • import android.content.Context;
  • import android.graphics.Color;
  • import android.graphics.drawable.ColorDrawable;
  • import android.os.Bundle;
  • import android.view.Gravity;
  • import android.view.Menu;
  • import android.view.View;
  • import android.view.animation.AlphaAnimation;
  • import android.view.animation.Animation;
  • import android.view.animation.ScaleAnimation;
  • import android.widget.PopupWindow;
  • import android.widget.RelativeLayout.LayoutParams;

  • public class MainActivity extends Activity {

  •     private PopupWindow pw;
  •     private AlphaAnimation aa;
  •     private View contentView;
  •     private ScaleAnimation sa;

  •     @Override
  •     protected void onCreate(Bundle savedInstanceState) {
  •         super.onCreate(savedInstanceState);
  •         setContentView(R.layout.activity_main);
  •         initPopup();
  •     }

  •     /**
  •      * 初始化弹出窗体
  •      */
  •     private void initPopup() {
  •         contentView = View.inflate(getApplicationContext(), R.layout.popup, null);

  •         pw = new PopupWindow(contentView , -2, -2);

  •        sa = new ScaleAnimation(
  •                1, 1,
  •                0, 1,
  •                Animation.RELATIVE_TO_SELF, 0.5f,
  •                Animation.RELATIVE_TO_SELF, 0f );
  •        sa.setDuration(1000);



  •     }

  •     /**
  •      * 弹出窗体
  •      * @param v
  •      */
  •     public void popupWindow(View v){
  •         if ( pw != null && pw.isShowing()) {
  •             pw.dismiss();
  •         } else {
  •             int[] location = new int[2];
  •             /**
  •              * 1,父组件
  •              * 2,对齐方式
  •              * 3, x坐标
  •              * 4,y坐标
  •              */
  •             v.getLocationInWindow(location );
  •             System.out.println("x:" + location[0] + ",y:" + location[1]);
  •             //设置弹出窗体的背景
  •             pw.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  •             contentView.startAnimation(sa);
  •             pw.showAtLocation(v, Gravity.LEFT | Gravity.TOP, location[0] + v.getWidth(), location[1] + v.getHeight());

  •         }
  •     }

  •     @Override
  •     protected void onDestroy() {
  •         // TODO Auto-generated method stub
  •         if (pw != null && pw.isShowing()) {
  •             pw.dismiss();
  •             pw = null;
  •         }
  •         super.onDestroy();
  •     }

  •     @Override
  •     public boolean onCreateOptionsMenu(Menu menu) {
  •         // Inflate the menu; this adds items to the action bar if it is present.
  •         getMenuInflater().inflate(R.menu.main, menu);
  •         return true;
  •     }
  • }
layoutactivity.xml





popup.xml












AndroidManifest.xml
















strings.xml




  •     PopupWindowDemo
  •     Settings
  •     Hello world!






来源:{http://bbs.zecoki.com/forum-70-1.html}
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




上一篇:【经验分享】用adb揪出安卓APP弹窗广告的原形
下一篇:获取安卓应用包名和入口 Activity
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

抖音账号
关注抖音
加入粉丝群,抽取论坛金币


Archiver|手机版|小黑屋|IT趣-GEEK社区

GMT+8, 2024-3-29 14:25 , Processed in 0.088089 second(s), 22 queries .

Powered by Discuz! X3.4. 技术支持 by 巅峰设计

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表