我卡在彈出窗口定位。我點擊一個按鈕來顯示我的彈出窗口。我希望它應該根據可用空間進行定位。另外,如果我的按鈕位於中心,它應該在按鈕下方。下面是我的這個東西的代碼。請讓我知道我錯在哪裏。謝謝。在Android中正確定位彈出窗口
mBtnPopUp.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
ListView mListView = (ListView) mPopUpView.findViewById(R.id.pop_up_list_view);
mListView.setAdapter(mPopupListAdapter);
Drawable drawable = getResources().getDrawable(android.R.drawable.alert_light_frame);
mPopupWindow.setBackgroundDrawable(drawable);
// mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
showLikeQuickAction(0, 0);
mPopupWindow.showAsDropDown(mBtnPopUp);
}
});
mPopupWindow.setOutsideTouchable(true);
public void showLikeQuickAction(int xOffset, int yOffset)
{
int[] location = new int[2];
mBtnPopUp.getLocationOnScreen(location);
Rect anchorRect = new Rect(location[0], location[1], location[0] + mBtnPopUp.getWidth(), location[1] + mBtnPopUp.getHeight());
mBtnPopUp.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
int rootWidth = mBtnPopUp.getMeasuredWidth();
int rootHeight = mBtnPopUp.getMeasuredHeight();
@SuppressWarnings("deprecation")
int screenWidth = mWindowManager.getDefaultDisplay().getWidth();
int xPos = screenWidth - rootWidth + xOffset;
int yPos = anchorRect.top - rootHeight + yOffset;
if(rootWidth > anchorRect.right - anchorRect.left)
{
// right
xPos = anchorRect.right - rootWidth;
}
else
{
// left
xPos = anchorRect.left + 15;
}
if(xPos + rootWidth > screenWidth)
xPos = screenWidth - rootWidth - 20;
// display on bottom
if(rootHeight > anchorRect.top)
{
yPos = anchorRect.bottom + yOffset;
// mPopupWindow.setAnimationStyle(R.style.Animations_GrowFromTop);
}
mPopupWindow.showAtLocation(mBtnPopUp, Gravity.NO_GRAVITY, xPos, yPos);
}
謝謝。
已經得到這個問題的解決?如果是,請提供給我解決方案 – 2013-11-11 06:04:52
你有解決方案嗎? – Dory 2013-12-12 04:54:29