PopupWindow的使用
PopupWindow还是比较常用的,具有对话框的特性,而且位置灵活,现在主要涉及的知识点在于popwindow的宽高测量上
1、PopWindow的打开关闭
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (pwMyPopWindow.isShowing()) {
popWindow.dismiss();// 关闭
} else {
popWindow.showAsDropDown(button);// 显示
}
}
});
2.PopWindow的创建
// 加载布局填充数据
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.task_detail_popupwindow, null); popupList = (ListView) layout.findViewById(R.id.lv_popup_list); popWindow = new PopupWindow(layout);
// 可以获得焦点,ListView才可点击 popWindow.setFocusable(true);
popupList.setAdapter(new SimpleAdapter(MyQQNews.this, moreList, R.layout.list_item_popupwindow, new String[] { "share_key" }, new int[] { R.id.tv_list_item })); popupList.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } }); // 控制popupwindow的宽度和高度自适应 popupList.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); popWindow.setWidth(popupList.getMeasuredWidth()); popWindow.setHeight(popupList.getMeasuredHeight()* NUM_OF_VISIBLE_LIST_ROWS); // 控制popupwindow点击屏幕其他地方消失 popWindow.setBackgroundDrawable(this.getResources().getDrawable( R.drawable.bg_popupwindow));// 设置背景图片,不能在布局中设置,要通过代码来设置 popWindow.setOutsideTouchable(true);// 触摸popupwindow外部,popupwindow消失。这个要求你的popupwindow要有背景图片才可以成功,如上
3、一些布局文件
<?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="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_list_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:minHeight="30dp"
android:minWidth="120dp"
android:textSize="20sp"
android:textColor="@color/popupwindow_list_item_text_selector"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ListView
android:id="@+id/lv_popup_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:listSelector="#00000000"
android:focusableInTouchMode="true"
/>
</LinearLayout>
浙公网安备 33010602011771号