创建一个popwindow 并动态设置pop的高度 限定pop高度
这里举个例子,pop里面放的是一个listview
直接上代码
SelectMedicalCasePopwindow。java
public class SelectMedicalCasePopwindow extends PopupWindow implements OnClickListener, AdapterView.OnItemClickListener { private TextView tv; private View mMenuView; private Context context; private Handler callback; private ListView mMyListView; private String[] items; private Button mButton; private ChatHistoryAdapter mAdapter; public SelectMedicalCasePopwindow(Activity con, List<String> itemList) { this(con, itemList.toArray(new String[itemList.size()])); } public SelectMedicalCasePopwindow(Activity con, String[] items) { super(con); this.context = con; this.items = items; int scrheight = DensityUtil.getScreenHeight(context) / 2; LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mMenuView = inflater.inflate(R.layout.select_medical_case_pop, null); mAdapter = new ChatHistoryAdapter(); mMyListView = (ListView) mMenuView.findViewById(R.id.bithday_layout); View listItem = mAdapter.getView(0, null, mMyListView); listItem.measure(0, 0); int relheight = listItem.getMeasuredHeight(); UtilsLog.d(scrheight); UtilsLog.d(relheight); this.setContentView(mMenuView); this.setWidth(LayoutParams.MATCH_PARENT); if(relheight * items.length > scrheight){ LayoutParams layoutParams = mMyListView.getLayoutParams(); layoutParams.width = LayoutParams.MATCH_PARENT; layoutParams.height = scrheight; mMyListView.setLayoutParams(layoutParams); //this.setHeight(scrheight); } else { } this.setHeight(LayoutParams.WRAP_CONTENT); // 设置SelectPicPopupWindow弹出窗体可点击 this.setFocusable(true); // 设置SelectPicPopupWindow弹出窗体动画效果 this.setAnimationStyle(R.style.AnimBottom); // 实例化一个ColorDrawable颜色为半透明 ColorDrawable dw = new ColorDrawable(0xb0000000); // 设置SelectPicPopupWindow弹出窗体的背景 this.setBackgroundDrawable(dw); // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框 mMenuView.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { int height = mMenuView.findViewById(R.id.bithday_layout_lin).getTop(); int y = (int) event.getY(); if (event.getAction() == MotionEvent.ACTION_UP) { if (y < height) { dismiss(); } } return true; } }); mButton = (Button) mMenuView.findViewById(R.id.btn_cancel); mButton.setOnClickListener(this); mMyListView.setAdapter(mAdapter); mMyListView.setOnItemClickListener(this); this.update(); } @Override public void showAtLocation(View parent, int gravity, int x, int y) { super.showAtLocation(parent, gravity, x, y); this.tv = (TextView) parent; } public void showAtLocationNoSetText(View parent, int gravity, int x, int y, Handler callback) { super.showAtLocation(parent, gravity, x, y); this.callback = callback; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_cancel: this.dismiss(); if (callback != null) { callback.sendEmptyMessage(0); } break; } } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (callback != null) { Message msg = callback.obtainMessage(1, position, 0); callback.sendMessage(msg); } dismiss(); } class ChatHistoryAdapter extends BaseAdapter { @Override public int getCount() { return items.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ChatHistoryHolder holder; if (convertView == null) { holder = new ChatHistoryHolder(); convertView = View.inflate(context, R.layout.view_chat_select_case_item, null); holder.clnic_doctor_item_time_tv = (TextView) convertView.findViewById(R.id.btn_myquestion_first); convertView.setTag(holder); } else { holder = (ChatHistoryHolder) convertView.getTag(); } holder.clnic_doctor_item_time_tv.setText(items[position]); return convertView; } class ChatHistoryHolder { TextView clnic_doctor_item_time_tv; } } }
select_medical_case_pop.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/bithday_layout_lin" android:background="@color/white"> <TextView android:id="@+id/choose_txt" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="13dip" android:text="选择病例" android:gravity="center" android:layout_centerInParent="true" android:textColor="@color/text_color_959595" android:textSize="@dimen/text_size_22px" /> <View android:layout_width="match_parent" android:layout_height="0.5dip" android:layout_below="@+id/choose_txt" android:background="@color/text_color_cccccc" /> </RelativeLayout> <LinearLayout android:id="@+id/bithday_layout_lin" android:layout_width="match_parent" android:layout_above="@+id/bithday_layout1" android:background="@color/white" android:layout_height="wrap_content"> <ListView android:id="@+id/bithday_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" android:divider="@color/text_color_cccccc" android:dividerHeight="0.1dp" /> </LinearLayout> <View android:id="@+id/bithday_layout1" android:layout_width="match_parent" android:layout_height="10dip" android:layout_above="@+id/layout_cancel" android:background="#f2f5f7" /> <LinearLayout android:id="@+id/layout_cancel" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:background="@color/white" android:gravity="center" android:orientation="horizontal"> <Button android:id="@+id/btn_cancel" android:layout_width="match_parent" android:layout_height="@dimen/height50" android:background="@drawable/freeclinics_btn_click_bg" android:gravity="center" android:text="取 消" android:textColor="@color/content" android:textSize="@dimen/text_size_20px" /> </LinearLayout> </RelativeLayout>
view_chat_select_case_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="55dip" android:background="@drawable/freeclinics_btn_click_bg"> <TextView android:id="@+id/btn_myquestion_first" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/freeclinics_btn_click_bg" android:gravity="center" android:padding="13dip" android:text="男" android:textColor="@color/content" android:textSize="@dimen/text_size_22px" /> </LinearLayout>