2013-04-29 27 views
4

我卡在彈出窗口定位。我點擊一個按鈕來顯示我的彈出窗口。我希望它應該根據可用空間進行定位。另外,如果我的按鈕位於中心,它應該在按鈕下方。下面是我的這個東西的代碼。請讓我知道我錯在哪裏。謝謝。在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); 
} 

謝謝。

+0

已經得到這個問題的解決?如果是,請提供給我解決方案 – 2013-11-11 06:04:52

+0

你有解決方案嗎? – Dory 2013-12-12 04:54:29

回答

1
public void downloadBtnSelected(View anchor) { 
    final ListPopupWindow lpw = new ListPopupWindow(this); 
    String[] data = { ".png", ".pdf", ".jpg", ".jpeg" }; 
    PopupAdapter pa = new PopupAdapter(data, this); 
    lpw.setAdapter(pa); 

    //setting up an anchor view 
    lpw.setAnchorView(anchor); 

    //Setting measure specifications. I'v used this mesure specs to display my 
    //ListView as wide as my anchor view is 
    lpw.setHeight(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT); 
    lpw.setWidth(anchor.getRight() - anchor.getLeft()); 

    // Background is needed. You can use your own drawable or make a 9patch. 
    // I'v used a custom btn drawable. looks nice. 
    lpw.setBackgroundDrawable(this.getResources().getDrawable(android.R.drawable.btn_default)); 

    // Offset between anchor view and popupWindow 
    lpw.setVerticalOffset(3); 

    lpw.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      // Our action..... 
      lpw.dismiss(); 

     } 
    }); 
    lpw.show(); 

} 

並與onClickListener按鈕來調用這個方法:

Button btn = new Button(this); 
btn.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     downloadBtnSelected(v); 
    } 
}); 

這個代碼讓我們您展現低於BTN 彈出寬度彈出將與按鈕一樣。 如果你想填充屏幕寬度 - 設置寬度= LinearLayout.MATCH_PARENT 彈出菜單將顯示錨視圖下方如果空間,超越下面是否有

0

不能有空格定義彈出使名爲

一個XML文件

cust_toast_layout.xml

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/toast_layout_root" 
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingTop="45dp" > 

    <TextView android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@android:color/white" 
     android:textSize="25sp" 
     android:textStyle="bold" 
     android:gravity="center" 
     android:paddingTop="200dp"/> 

</LinearLayout> 

然後在Java文件要將這個彈出添加以下代碼

LayoutInflater inflater = getLayoutInflater(); 
View layout = inflater.inflate(R.layout.cust_toast_layout, 
           (ViewGroup)findViewById(R.id.toast_layout_root)); 



TextView text = (TextView) layout.findViewById(R.id.text); 
//text.setText("Excellent !!!"); 
text.setBackgroundResource(R.drawable.right_ans); 
Toast toast = new Toast(getApplicationContext()); 
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); 
toast.setDuration(Toast.LENGTH_SHORT); 
toast.setView(layout); 
toast.show(); 

在buttonclick上,您可以調用一個方法,並在該方法中包含此代碼。 您可以根據您的要求通過在xml文件中編輯進行更改。

+0

感謝Zeel的迴應,但我認爲你沒有得到我的問題。我正在使用彈出式窗口,我想將它放在按鈕w.r.t的下方或上方。屏幕上按鈕上的位置。爲前。如果我的按鈕位於屏幕的中心以上,我的彈出窗口應該出現在按鈕的底部。 – Amrut 2013-04-29 07:28:45

+0

你的按鈕定位? – Zeel 2013-04-29 10:06:36

+0

您可以在條件運算符中爲此設置權重屬性。但對於彈出我還沒有嘗試。如果你嘗試,然後讓我知道它是否在工作。 – Zeel 2013-04-29 10:12:46

1

我知道它很安靜,但我希望它能幫助別人。

LayoutInflater layoutInflater = getLayoutInflater();     
int popupWidth = 500;//ViewGroup.LayoutParams.WRAP_CONTENT; 
int popupHeight = ViewGroup.LayoutParams.WRAP_CONTENT; 
popupView = layoutInflater.inflate(R.layout.main, null); 

PopupWindow attachmentPopup = new PopupWindow(this); 
attachmentPopup.setFocusable(true); 
attachmentPopup.setWidth(popupWidth); 
attachmentPopup.setHeight(popupHeight);   
attachmentPopup.setContentView(popupView); 
attachmentPopup.setBackgroundDrawable(new BitmapDrawable()); 
attachmentPopup.showAsDropDown(v, -5, 0); 

它會根據您的按鈕的位置來定位您的彈出窗口。

0

該代碼會顯示在特定的你中心視圖完美彈出

// click event of specific view 
    @Override 
    public void onClick(View v) { 
     super.onClick(v); 
     if (v == lin_filterActionbar) { 
      PopupWindow popupwindow_obj; // create object 
      popupwindow_obj = dialog_AllergiesSelect(RR_HomeActivity.this,lin_filterActionbar); 
      popupwindow_obj.showAsDropDown(lin_filterActionbar); 

     } 
    } 



private PopupWindow dialog_AllergiesSelect(Context context,LinearLayout lin) { 

     final PopupWindow dialog_AllergiesSelect = new PopupWindow(this); 

     View v = View.inflate(context, R.layout.dialog_filter_actionbar, null); 
     dialog_AllergiesSelect.setContentView(v); 
     dialog_AllergiesSelect.setWidth(WindowManager.LayoutParams.WRAP_CONTENT); 
     dialog_AllergiesSelect.setHeight(WindowManager.LayoutParams.WRAP_CONTENT); 
     dialog_AllergiesSelect.setFocusable(true); 

//  TextView lis_testSelection = (TextView) v.findViewById(R.id.dialogfilter_txt_filter); 
//  LinearLayout.LayoutParams lin_laLayoutParams = new LinearLayout.LayoutParams((int) (sunApplication.getDeviceWidth() - (sunApplication.getDeviceScale() * dialog_width_dvide_allerges_spinner_width)), (int) (sunApplication.getDeviceHeight() - (sunApplication.getDeviceScale() * dialog_width_dvide_allerges_height))); 
//  lis_testSelection.setLayoutParams(lin_laLayoutParams); 

     // Some offset to align the popup a bit to the right, and a bit down, relative to button's position. 
     int OFFSET_X = 0; 
     int OFFSET_Y = 0; 

     // Clear the default translucent background 
     dialog_AllergiesSelect.setBackgroundDrawable(new BitmapDrawable()); 

     // Displaying the popup at the specified location, + offsets. 
     //dialog_AllergiesSelect.showAtLocation(v, Gravity.C, p.x + OFFSET_X, p.y + OFFSET_Y); 

     Rect location = locateView(lin); 

     dialog_AllergiesSelect.showAtLocation(v, Gravity.TOP|Gravity.CENTER, 0, location.bottom); 

     // Getting a reference to Close button, and close the popup when clicked. 

     return dialog_AllergiesSelect; 

    } 
    public static Rect locateView(View v) 
    { 
     int[] loc_int = new int[2]; 
     if (v == null) return null; 
     try 
     { 
      v.getLocationOnScreen(loc_int); 
     } catch (NullPointerException npe) 
     { 
      //Happens when the view doesn't exist on screen anymore. 
      return null; 
     } 
     Rect location = new Rect(); 
     location.left = loc_int[0]; 
     location.top = loc_int[1]; 
     location.right = location.left + v.getWidth(); 
     location.bottom = location.top + v.getHeight(); 
     return location; 
    }