package com.loaderman.popupwindowdemo;import android.app.Activity;import android.graphics.Color;import android.graphics.drawable.ColorDrawable;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.PopupWindow;public class MainActivity extends Activity { private Button btnPop; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnPop = (Button) findViewById(R.id.btn_Pop); } public void showPopupWindow(View v) { View view = View.inflate(this, R.layout.layout_pop, null); PopupWindow popup = new PopupWindow(view, v.getWidth(), 150, true);//设置尺寸及获取焦点 //必须设置背景色,不然不会消失 popup.setBackgroundDrawable(new ColorDrawable(Color.GRAY));//设置背景颜色 // 设置进入退出动画 popup.setAnimationStyle(R.style.PopAnimation); // 显示在某个控件的正下方 popup.showAsDropDown(btnPop, 0, 0); //显示在屏幕的位置 // popup.showAtLocation(rlRoot, Gravity.LEFT + Gravity.TOP, 0,0); }}
activity_main.xml
layout_pop.xml
在values/style中:
在res/anim文件下创建进入退出动画
pop_enter.xml
pop_exit.xml
效果图: