【android】让popupwindow显示在view的上方并与该view水平居中对齐
2014-08-19 23:42
555 查看
首先,废话少说,先上效果图:
代码:
Demo下载:http://download.csdn.net/detail/u011494050/7787359
代码:
public class MainActivity extends Activity implements OnClickListener{ private Button showBtn1; private Button showBtn2; @Override protected 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); } @Override public void onClick(View v) { // TODO Auto-generated method stub View 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
相关文章推荐
- 【android】让popupwindow显示在view的上方并与该view水平居中对齐
- 让popupwindow显示在view的上方并与该view水平居中对齐
- 让popupwindow显示在view的上方并与该view水平居中对齐
- android 如何让PopupWindow显示在ImageView的下方
- Android自定义PopupWindow显示在控件上方或者下方
- Android自定义PopupWindow显示在控件上方或者下方
- Android自定义PopupWindow显示在控件上方或者下方
- Android自定义PopupWindow显示在控件上方或者下方
- android popupwindow在屏幕内水平居中
- PopupWindow封装-view上方显示和下方显示
- Android中图片的展示之Webview居中显示图片
- android 之popupWindow 在指定位置上的显示
- android textview设置居中循环显示,
- android 之popupWindow 在指定位置上的显示 .
- android webView加载gif图片并且居中显示
- android 之popupWindow 在指定位置上的显示
- Android中PopupWindow显示在指定位置
- 【Android开发学习33】PopupWindow之显示顶层对话框
- Android UI 如何让TextView中的文字居中显示
- android开发关于popupwindow显示关闭的笔记