烏爾y位置這應該工作
//Activity windows height
int totalHeight = getWindowManager().getDefaultDisplay().getHeight();
int[] location = new int[2];
v.getLocationOnScreen(location);
位置數組應該具有視圖的x和y值。 'v'是在onItemClickListener上傳遞的視圖對象。
我添加了一些用於我的項目的部分。這可能會有所幫助。我在列表視圖的頂部有一個操作欄,這段代碼看起來很好。
要求是將一個小菜單放在列表項的上面或下面。所以當選擇一個項目時,我會檢查選定的列表項目是否在屏幕的上半部分,如果是這樣的話,將菜單放在列表項目的下面,否則將它放在列表項目的頂部。 下面的代碼
列表項點擊代碼
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position
, long id) {
showQuickActionMenu(position,view);
}
});
private void showQuickActionMenu(int pos, View v){
LayoutInflater inflater =
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//This is just a view with buttons that act as a menu.
View popupView = inflater.inflate(R.layout.ticket_list_menu, null);
popupView.findViewById(R.id.menu_view).setTag(pos);
popupView.findViewById(R.id.menu_change_status).setTag(pos);
popupView.findViewById(R.id.menu_add_note).setTag(pos);
popupView.findViewById(R.id.menu_add_attachment).setTag(pos);
window = PopupHelper.newBasicPopupWindow(TicketList.this);
window.setContentView(popupView);
int totalHeight = getWindowManager().getDefaultDisplay().getHeight();
int[] location = new int[2];
v.getLocationOnScreen(location);
if (location[1] < (totalHeight/2.0)) {
PopupHelper.showLikeQuickAction(window, popupView, v
, getWindowManager(),0,0,PopupHelper.UPPER_HALF);
} else {
PopupHelper.showLikeQuickAction(window, popupView, v
, getWindowManager(),0, 0,PopupHelper.LOWER_HALF);
}
}
這是我使用
public class PopupHelper {
public static final int UPPER_HALF = 0;
public static final int LOWER_HALF = 1;
public static PopupWindow newBasicPopupWindow(Context context) {
final PopupWindow window = new PopupWindow(context);
// when a touch even happens outside of the window
// make the window go away
window.setTouchInterceptor(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_OUTSIDE) {
window.dismiss();
return true;
}
return false;
}
});
window.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
window.setTouchable(true);
window.setFocusable(true);
window.setOutsideTouchable(true);
window.setBackgroundDrawable(
new ColorDrawable(android.R.color.darker_gray));
return window;
}
/**
* Displays like a QuickAction from the anchor view.
*
* @param xOffset
* offset in the X direction
* @param yOffset
* offset in the Y direction
*/
public static void showLikeQuickAction(PopupWindow window, View root,
View anchor, WindowManager windowManager, int xOffset
,int yOffset,int section) {
//window.setAnimationStyle(R.style.Animations_GrowFromBottomRight);
int[] location = new int[2];
anchor.getLocationOnScreen(location);
Rect anchorRect = new Rect(location[0], location[1], location[0] +
anchor.getWidth(), location[1] + anchor.getHeight());
root.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
int rootWidth = root.getMeasuredWidth();
int rootHeight = root.getMeasuredHeight();
int screenWidth = windowManager.getDefaultDisplay().getWidth();
int screenHeight = windowManager.getDefaultDisplay().getHeight();
int xPos = ((screenWidth - rootWidth)/2) + xOffset;
int yPos = anchorRect.top - rootHeight + yOffset;
xPos = (screenWidth - rootWidth);
if(section == UPPER_HALF){
yPos = anchorRect.top + anchor.getMeasuredHeight();
} else {
yPos = anchorRect.top - rootHeight;
}
window.showAtLocation(anchor, Gravity.NO_GRAVITY, xPos, yPos);
}
}
謝謝你,但我試過這個,它只給了我相對的位置。所以第一項是列表是0,0。這很接近,因爲我認爲這也是解決方案。我相信我在某處有一些翻譯問題。 – 2012-07-24 14:57:31
可以把你的佈局xml。我有點相同的要求,但我的佈局只有一個列表視圖。 – blessenm 2012-07-24 14:59:50
我已經更新了我的答案。 – blessenm 2012-07-24 15:45:56