我正在尝试在PopupWindow中显示ListView的内容,这些内容随着List的填充而增长。它工作正常,除了元素随着列表的增长而移动,直到它停止添加新元素。当窗口在屏幕上向下增长时,有没有办法保持窗口的位置固定?我是Android编程的新手,所以任何输入都是值得赞赏的。谢谢!
private void initView(View v) {
LayoutInflater inflater = (LayoutInflater)
getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.popup_layout, null);
DisplayMetrics displaymetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().
getMetrics(displaymetrics);
int width = (int) ((int)displaymetrics.widthPixels * 0.9);
popupWindow = new PopupWindow(popupView,width,
ViewGroup.LayoutParams.WRAP_CONTENT);
m = (MainActivity) getActivity();
m.layout_main.getForeground().setAlpha(220);
btnScan = (Button) popupView.findViewById(R.id.scan);
btncancel = (Button) popupView.findViewById(R.id.close);
popupWindow.setOutsideTouchable(false);
popupWindow.setFocusable(true);
int loc_int[] = new int[2];
if (v == null)
try
{
v.getLocationOnScreen(loc_int);
} catch (NullPointerException npe)
{
//when the view doesn't exist on screen anymore.
}
Rect location = new Rect();
location.left = loc_int[0];
location.top = loc_int[1] ;
location.right = location.left + v.getWidth()/2;
location.bottom = location.top - view2.getHeight()/2 ;
popupWindow.showAtLocation(view2, Gravity.CENTER,
view2.getWidth()/2, location.top);
popupWindow.showAsDropDown(view2);
listDevicesFound = (ListView) popupView.findViewById(R.id.devicelist);
btAdapter = BluetoothAdapter.getDefaultAdapter();
adtDevice = new ArrayAdapter<String>(getActivity(),R.layout.list_text,
lsDevice);
listDevicesFound.setAdapter(adtDevice);
listDevicesFound.setOnItemClickListener(new onIntemClick());
}
答案 0 :(得分:0)
我最终使用以下方法获得的宽度和高度的一小部分来修复窗口的宽度和高度: DisplayMetrics metrics = this.getResources()。getDisplayMetrics(); MyApplication.width = metrics.widthPixels; MyApplication.height = metrics.heightPixels;
使用固定高度,PopupWindow不会再添加新的List。