ListPopupWindow(v7包下) 类似于内置了一个 ListView 的 PopupWindow,对子菜单的操作更加灵活。
示意图.png代码实现过程
private void showListPopupWindow(View view) {
final String items [] = {"0","1","2","3","4","5","6"};
ListPopupWindow listPopupWindow = new ListPopupWindow(this);
// 设置适配器
listPopupWindow.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items));
// 设置 item 的监听事件
listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this, items[i], Toast.LENGTH_SHORT).show();
}
});
// 对话框宽高
listPopupWindow.setWidth(500);
listPopupWindow.setHeight(600);
// 设置 listPopupWindow 的锚点,即弹出的对话框的位置相对于当前 view 的位置
listPopupWindow.setAnchorView(view);
// 距锚 view 的距离
listPopupWindow.setHorizontalOffset(40);
listPopupWindow.setVerticalOffset(100);
// 设置当用户触碰到对话框以外的区域,对话框是否消失
listPopupWindow.setModal(false);
listPopupWindow.show();
}
网友评论