首先,废话少说,先上效果图:



代码:

public class MainActivity extends Activity implements OnClickListener{private Button showBtn1;private Button showBtn2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);showBtn1 = (Button) findViewById(R.id.showBtn1);showBtn2 = (Button) findViewById(R.id.showBtn2);showBtn1.setOnClickListener(this);showBtn2.setOnClickListener(this);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubView popupView = LayoutInflater.from(this).inflate(R.layout.popup, null);PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);popupWindow.setBackgroundDrawable(new BitmapDrawable());popupView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);int popupWidth = popupView.getMeasuredWidth();int popupHeight =  popupView.getMeasuredHeight();int[] location = new int[2];switch (v.getId()) {case R.id.showBtn1:    v.getLocationOnScreen(location);    popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, (location[0]+v.getWidth()/2)-popupWidth/2,    location[1]-popupHeight);break;case R.id.showBtn2:    v.getLocationOnScreen(location);    popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, (location[0]+v.getWidth()/2)-popupWidth/2,    location[1]-popupHeight);break;default:break;}}}


Demo下载:http://download.csdn.net/detail/u011494050/7787359

更多相关文章

  1. android EditText设置不可写
  2. android 使用html5作布局文件: webview跟javascript交互
  3. android studio调试c/c++代码
  4. IM-A820L限制GSM,WCDMA上网的原理(其他泛泰机型可参考)7.13
  5. 锁屏界面
  6. android(NDK+JNI)---Eclipse+CDT+gdb调试android ndk程序
  7. Android(安卓)version and Linux Kernel version
  8. Android(安卓)闹钟管理类的使用
  9. Android学习篇之Menu的使用

随机推荐

  1. android 开发,多个线程共用一个handler
  2. Android 定制自己的launcher
  3. android menu 实现动态修改menu .
  4. Android开发的文件格式概述
  5. android 自定义view添加自定义xml属性
  6. Android之——AIDL小结
  7. Bug系列------------第一次开机结束无法
  8. Android 拼接两个图片
  9. ubuntu11.10中配置android 环境
  10. 去掉移动网页、android webview 加载网页