1.布局
<RelativeLayout android:id="@+id/call_service_data_list" android:layout_alignParentBottom="true" android:layout_width="fill_parent" android:layout_height="150sp"> <TextView android:id="@+id/call_server_item_text" android:layout_width="fill_parent" android:paddingTop="3dp" android:paddingLeft="3dp" android:layout_height="wrap_content" android:textSize="@dimen/textsize_28" android:text="已完成服务"/> <ImageView android:id="@+id/divider_line" android:layout_below="@+id/call_server_item_text" android:layout_width="fill_parent" android:padding="3dp" android:layout_height="0.1dp" android:background="#99CC33" /> <ListView android:layout_below="@+id/divider_line" android:id="@+id/service_listview" android:layout_width="fill_parent" android:divider="#99CC33" android:dividerHeight="0.1dp" android:layout_height="130sp" /> </RelativeLayout>2.listitem
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <RelativeLayout android:id="@+id/call_server_item_content" android:layout_alignParentBottom="true" android:paddingTop="3dp" android:layout_width="fill_parent" android:layout_height="40dp"> <TextView android:id="@+id/service_contnt" android:layout_centerVertical="true" android:paddingLeft="3dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="@dimen/textsize_30" android:textColor="@color/black" /> <TextView android:id="@+id/finish_time" android:layout_centerVertical="true" android:paddingRight="3dp" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="@dimen/textsize_30" android:textColor="@color/black" /> </RelativeLayout> </RelativeLayout>
3.Activity
package com.android.hotelcontrol.callService; public class CallServiceActivity extends Activity { private static ListView lv; private SimpleAdapter adapter; private Button requestBtn; private PopupWindow mPopupWindow; // 屏幕的width private int mScreenWidth; // 屏幕的height private int mScreenHeight; // PopupWindow的width private int mPopupWindowWidth; // PopupWindow的height private int mPopupWindowHeight; @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_call_service); lv = (ListView) findViewById(R.id.service_listview); adapter = new SimpleAdapter(this,Servicedata(),R.layout.call_service_list_item,new String[]{"content","time"},new int[]{R.id.service_contnt,R.id.finish_time}); lv.setAdapter(adapter); requestBtn = (Button) findViewById(R.id.send_call_requset); } // demo 静态数据 private static List<Map<String,String>> Servicedata(){ List<Map<String , String>> datalist = new ArrayList<Map<String , String>>(); Map<String , String> map = new HashMap<>(); map.put("content","需:鸡尾酒X1"); map.put("time","2015/11/16 11:55"); datalist.add(map); Map<String , String> map1 = new HashMap<>(); map1.put("content", "需:雨伞X1"); map1.put("time", "2015/11/14 11:23"); datalist.add(map1); Map<String , String> map3 = new HashMap<>(); map3.put("content","需:人工服务"); map3.put("time", "2015/11/10 11:44"); datalist.add(map3); return datalist; } public void onClick(View v){ switch (v.getId()) { case R.id.send_call_requset: this.requestBtn.setVisibility(View.GONE); getPopupWindowInstance(); mPopupWindow.showAtLocation(v, Gravity.CENTER, 0, 0); break; case R.id.return_main_interface: finish(); default: break; } } /* * 获取PopupWindow实例 */ private void getPopupWindowInstance() { if (null != mPopupWindow) { mPopupWindow.dismiss(); return; } else { initPopuptWindow(); } } /* * 创建PopupWindow */ private void initPopuptWindow() { LayoutInflater layoutInflater = LayoutInflater.from(this); View popupWindow = layoutInflater.inflate(R.layout.popup_window, null); final float scale = this.getResources().getDisplayMetrics().density; Log.d("PopupWindowDemo", "scale为" + scale); //将dp转化为像素 int height =(int)(scale*260+0.5f); Log.d("PopupWindowDemo","弹出窗的高度为"+height); // 获取屏幕和PopupWindow的width和height mScreenWidth = getWindowManager().getDefaultDisplay().getWidth(); mScreenHeight = getWindowManager().getDefaultDisplay().getHeight(); // 创建一个PopupWindow // 参数1:contentView 指定PopupWindow的内容 // 参数2:width 指定PopupWindow的width // 参数3:height 指定PopupWindow的height mPopupWindow = new PopupWindow(popupWindow, mScreenWidth, height); mPopupWindowWidth = mPopupWindow.getWidth(); mPopupWindowHeight = mPopupWindow.getHeight(); } }