PopupWindow实现弹出窗口

本博客展示了如何使用Android的PopupWindow实现弹出窗口,用于在点击ListView的每一个item时,显示对应的用户详细信息。包括姓名、年龄、生日、电话号码、部门等。通过XML布局和Java代码实现弹出窗口的动态加载和显示,以及关闭机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

点击ListView的每一个item,将弹出窗口显示item详情 
 
主要代码:
Java代码   收藏代码
  1. private void openPopupwin() {  
  2.         LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);  
  3.         ViewGroup rootView = (ViewGroup) mLayoutInflater.inflate(R.layout.person_info_pop, nulltrue);  
  4.           
  5.         TextView person_popup_name = (TextView)rootView.findViewById(R.id.person_popup_name);  
  6.         TextView person_popup_realName = (TextView)rootView.findViewById(R.id.person_popup_realName);  
  7.         TextView person_popup_age = (TextView)rootView.findViewById(R.id.person_popup_age);  
  8.         TextView person_popup_birthday = (TextView)rootView.findViewById(R.id.person_popup_birthday);  
  9.         TextView person_popup_phone = (TextView)rootView.findViewById(R.id.person_popup_phone);  
  10.         TextView person_popup_deptId = (TextView)rootView.findViewById(R.id.person_popup_deptId);  
  11.         Button btn_popup_close = (Button)rootView.findViewById(R.id.btn_popup_close);  
  12.         ImageView person_popup_head = (ImageView)rootView.findViewById(R.id.person_popup_head);  
  13.           
  14.         try {  
  15.             person_popup_name.setText(selectedperson.getString("name"));  
  16.             person_popup_realName.setText(selectedperson.getString("realName"));  
  17.             person_popup_age.setText(selectedperson.getString("age"));  
  18.             person_popup_birthday.setText(selectedperson.getString("birthday"));  
  19.             person_popup_phone.setText("13880012450");//还未添加字段  
  20.             person_popup_deptId.setText("产品事业部");  
  21.             person_popup_head.setBackgroundDrawable(ImageUtil.geRoundDrawableFromUrl(HttpUtil.BASE_URL+selectedperson.getString("headPhotoPath"), 10));  
  22.         } catch (JSONException e) {  
  23.             e.printStackTrace();  
  24.         } catch (Exception e) {  
  25.             e.printStackTrace();  
  26.         }  
  27.           
  28.         popupWindow = new PopupWindow(rootView, LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT, true);  
  29.         popupWindow.setBackgroundDrawable(new BitmapDrawable());  
  30.         popupWindow.setAnimationStyle(R.style.PopupAnimation);  
  31.         popupWindow.showAtLocation(root, Gravity.CENTER | Gravity.CENTER, 00);  
  32.         popupWindow.update();  
  33.           
  34.         btn_popup_close.setOnClickListener(new OnClickListener() {  
  35.             @Override  
  36.             public void onClick(View v) {  
  37.                 popupWindow.dismiss();  
  38.             }  
  39.         });  
  40.     }  

弹出窗口布局文件: 
Java代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="wrap_content" android:gravity="center"  
  4.     android:layout_height="wrap_content" android:layout_gravity="center" android:padding="5.0dip" android:layout_margin="5.0dip"  
  5.     android:background="@drawable/popup_corner">  
  6.     <RelativeLayout android:id="@+id/popup_top_bar" android:layout_alignParentTop="true" android:layout_width="fill_parent" android:layout_height="wrap_content">  
  7.         <TextView android:text="用户详细信息" android:textColor="#760606" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16.0sp" android:layout_alignParentLeft="true"/>  
  8.         <Button android:id="@+id/btn_popup_close" android:background="@drawable/popup_close" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true"/>  
  9.     </RelativeLayout>  
  10.     <ImageView android:id="@+id/person_popup_head" android:scaleType="fitXY" android:layout_marginTop="5.0dip" android:layout_below="@id/popup_top_bar" android:layout_width="120.0dip" android:layout_height="120.0dip"/>  
  11.     <TableLayout android:id="@+id/person_form" android:layout_marginLeft="2.0dip" android:layout_marginTop="5.0dip" android:layout_toRightOf="@id/person_popup_head" android:layout_below="@id/popup_top_bar" android:layout_width="wrap_content" android:layout_height="wrap_content">  
  12.         <TableRow >  
  13.             <TextView android:layout_width="60.0dip" android:gravity="right" android:textColor="#074f92" android:layout_height="wrap_content" android:text="账号:"/>  
  14.             <TextView android:id="@+id/person_popup_name" android:textColor="#074f92" android:layout_marginLeft="15.0dip" android:layout_width="wrap_content" android:layout_height="wrap_content"/>  
  15.         </TableRow>  
  16.         <TableRow >  
  17.             <TextView android:layout_width="60.0dip" android:gravity="right" android:textColor="#074f92" android:layout_height="wrap_content" android:text="姓名:"/>  
  18.             <TextView android:id="@+id/person_popup_realName" android:textColor="#074f92" android:layout_marginLeft="15.0dip" android:layout_width="wrap_content" android:layout_height="wrap_content"/>  
  19.         </TableRow>  
  20.         <TableRow >  
  21.             <TextView android:layout_width="60.0dip" android:gravity="right" android:textColor="#074f92" android:layout_height="wrap_content" android:text="年龄:"/>  
  22.             <TextView android:id="@+id/person_popup_age" android:textColor="#074f92" android:layout_marginLeft="15.0dip" android:layout_width="wrap_content" android:layout_height="wrap_content"/>  
  23.         </TableRow>  
  24.         <TableRow >  
  25.             <TextView android:layout_width="60.0dip" android:gravity="right" android:textColor="#074f92" android:layout_height="wrap_content" android:text="生日:"/>  
  26.             <TextView android:id="@+id/person_popup_birthday" android:textColor="#074f92" android:layout_marginLeft="15.0dip" android:layout_width="wrap_content" android:layout_height="wrap_content"/>  
  27.         </TableRow>  
  28.         <TableRow >  
  29.             <TextView android:layout_width="60.0dip" android:gravity="right" android:textColor="#074f92" android:layout_height="wrap_content" android:text="电话号码:"/>  
  30.             <TextView android:id="@+id/person_popup_phone" android:textColor="#074f92" android:layout_marginLeft="15.0dip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:autoLink="phone"/>  
  31.         </TableRow>  
  32.         <TableRow >  
  33.             <TextView android:layout_width="60.0dip" android:gravity="right" android:textColor="#074f92" android:layout_height="wrap_content" android:text="部门:"/>  
  34.             <TextView android:id="@+id/person_popup_deptId" android:textColor="#074f92" android:layout_marginLeft="15.0dip" android:layout_width="wrap_content" android:layout_height="wrap_content"/>  
  35.         </TableRow>  
  36.     </TableLayout>  
  37. </RelativeLayout>  

popup_enter.xml 
Java代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <scale android:fromXScale="0.6" android:toXScale="1.0"  
  4.         android:fromYScale="0.6" android:toYScale="1.0" android:pivotX="50%"  
  5.         android:pivotY="50%" android:duration="1000" />  
  6.   
  7.     <alpha android:interpolator="@android:anim/decelerate_interpolator"  
  8.         android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="1000" />  
  9. </set>  

popup_exit.xml 
Java代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <scale  
  4.         android:fromXScale="1.0"  
  5.         android:toXScale="0.5"  
  6.         android:fromYScale="1.0"  
  7.         android:toYScale="0.5"  
  8.         android:pivotX="50%"  
  9.         android:pivotY="50%"  
  10.         android:duration="500" />  
  11.     <alpha  
  12.         android:interpolator="@android:anim/accelerate_interpolator"  
  13.         android:fromAlpha="1.0"  
  14.         android:toAlpha="0.0"  
  15.         android:duration="500" />  
  16. </set>  

popup_corner.xml 
Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">  
  3.     <gradient android:startColor="#cbeafe" android:endColor="#ffffff" android:angle="90" />  
  4.     <!--    
  5.     <stroke android:dashWidth="2dp" android:dashGap="2dp" android:width="2dp" android:color="#FF00ff00"/>-->  
  6.     <!--描边-->  
  7.     <corners android:bottomRightRadius="5dp" android:bottomLeftRadius="5dp" android:topLeftRadius="5dp" android:topRightRadius="5dp" />  
  8.     <!--设置圆角-->  
  9. </shape>  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值