码迷,mamicode.com
首页 > 其他好文 > 详细

快速集成:自定义Popup

时间:2015-08-18 16:09:41      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:


  1. /**自定义Popup:START**/
  2. private PopupWindow mPopupWindow;
  3. private void initPopup() {
  4. /*生成popup的视图*/
  5. View popupWindow = createPopupView();
  6. /*设置popup的宽度和高度*/
  7. int width = getResources().getDisplayMetrics().widthPixels;//得到当前显示设备的宽度,单位是像素
  8. int height = getResources().getDisplayMetrics().heightPixels;//得到当前显示设备的宽度,单位是像素
  9. if(dataSize>5){
  10. mPopupWindow = new PopupWindow(popupWindow, 540 * width / 645,1 * height / 2, true);
  11. }else {
  12. mPopupWindow = new PopupWindow(popupWindow, 540 * width / 645,WindowManager.LayoutParams.WRAP_CONTENT, true);
  13. }
  14. /*配置pop*/
  15. mPopupWindow.setTouchable(true);
  16. mPopupWindow.setOutsideTouchable(true);//设置是否点击PopupWindow外退出PopupWindow
  17. mPopupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));//设置背景为空Drawable对象,如不设置,则无法退出
  18. mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
  19. mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
  20. /*popup的隐藏监听,恢复主界面亮度*/
  21. mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
  22. @Override
  23. public void onDismiss() {
  24. WindowManager.LayoutParams params = getWindow().getAttributes();
  25. params.alpha = 1.0f;
  26. getWindow().setAttributes(params);
  27. }
  28. });
  29. }
  30. private void showPopup() {
  31. /*背景变暗*/
  32. WindowManager.LayoutParams params = getWindow().getAttributes();//创建当前界面的一个参数对象
  33. params.alpha = 0.7f;
  34. getWindow().setAttributes(params);
  35. /*显示popup*/
  36. mPopupWindow.showAtLocation(mainView, Gravity.CENTER, 0, 0); //参数:父View对象,重心,x轴偏移量,y轴的偏移量
  37. }
  38. private View createPopupView() {
  39. /*加载视图*/
  40. View popupWindow = LayoutInflater.from(this).inflate(R.layout.popup_borrowreason, null);
  41. ListView listview = (ListView) popupWindow.findViewById(R.id.listview);
  42. /*绑定数据*/
  43. ArrayList<String> list = getBorrowReason();
  44. ArrayAdapter<String> adapter;
  45. adapter= new ArrayAdapter(this,R.layout.item_borrowreason,R.id.textView,list);//使用自定义布局
  46. listview.setAdapter(adapter);
  47. /*设置监听*/
  48. listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  49. @Override
  50. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  51. AbToastUtil.showToast(context, "你点击了:popup=" + position);
  52. }
  53. });
  54. return popupWindow;
  55. }
  56. int dataSize;
  57. private ArrayList<String> getBorrowReason() {
  58. ArrayList<String> list = new ArrayList<String>();
  59. list.add("没钱买吃的");
  60. list.add("没钱买喝的");
  61. list.add("没钱买玩的");
  62. list.add("没钱买乐的");
  63. dataSize=list.size();
  64. return list;
  65. }
  66. /**自定义Popup:END**/
使用的布局文件
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:padding="5dp"
  6. android:background="@drawable/shape_white_bg04"
  7. android:orientation="vertical">
  8. <LinearLayout
  9. android:background="@drawable/shape_white_bg03"
  10. android:orientation="vertical"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content">
  13. <TextView
  14. android:id="@+id/tv01"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:layout_margin="10dp"
  18. android:text="借款原因"
  19. android:textColor="#0099FF"
  20. android:textSize="20dp" />
  21. <View
  22. android:layout_width="match_parent"
  23. android:layout_height="1dp"
  24. android:background="#cdcdcd" />
  25. <ListView
  26. android:listSelector="#ffffff"
  27. android:scrollbars="none"
  28. android:id="@+id/listview"
  29. android:layout_width="match_parent"
  30. android:layout_height="wrap_content"
  31. android:layout_margin="5dp"></ListView>
  32. <View
  33. android:layout_width="match_parent"
  34. android:layout_height="5dp"/>
  35. </LinearLayout>
  36. </LinearLayout>









快速集成:自定义Popup

标签:

原文地址:http://www.cnblogs.com/yutianran/p/4739396.html

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