2013-04-29 50 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; 
    }