2017-03-02 68 views
0

我想在同一位置設置對話框的位置,在其clicked..like這個如何讓GridView的項目位置的x,y正確

enter image description here

,但我得到這個

enter image description here

這裏是我的代碼來定位對話框..

public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
     float positionX = v.getX(); 
     float positionY = v.getY(); 

     LinearLayout container = (LinearLayout) gvGrid.getFocusedChild(); 

     dialog = new Dialog(CW_Emergency.this); 
     dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     dialog.setContentView(R.layout.view_pager_layout); 
     dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); 

     //position of popup 
     WindowManager.LayoutParams params = dialog.getWindow().getAttributes(); 
     params.x = (int) positionX; 
     params.y = (int) positionY; 

     ViewPagerAdapter adapter = new ViewPagerAdapter(container, CW_Emergency.this); 
     viewPager = (ViewPager) dialog.findViewById(R.id.view_pager); 

     dialog.show(); 



    } 

好心告訴我如何正確定位所有屏幕尺寸

+0

您是否嘗試過沒有設置對話框的位置? – shahid17june

+0

是的..它出現在屏幕中心 – Asad

回答

0

您可以使用PopupWindow。

mPopupWindow = new PopupWindow(
        customView, 
        LayoutParams.WRAP_CONTENT, 
        LayoutParams.WRAP_CONTENT 
      ); 

,並顯示它

mPopupWindow.showAsDropDown(ItemInGridView, 0, -1 * ItemInGridView.getHeight(), Gravity.LEFT); 
0

據我要在正確的位置PopupMenu的顯示對話框會更方便和簡單。爲了實現PopupMenu的...

  1. 首先,你必須創建內部菜單資源文件夾菜單資源文件

    <?xml version="1.0" encoding="utf-8"?> 
    <menu xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:id="@+id/menu_option1" 
        android:title="Option 1" 
        /> 
    <item android:id="@+id/menu_option2" 
        android:title="Option 2" 
        /> 
    </menu> 
    
  2. 實現你onItemClick()內將以下代碼方法

    public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
    
    PopupMenu popup = new PopupMenu(this, v); 
    // Inflate the menu from xml 
    popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu()); 
    // Setup menu item selection 
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 
        public boolean onMenuItemClick(MenuItem item) { 
         switch (item.getItemId()) { 
         case R.id.menu_option1: 
          Toast.makeText(MainActivity.this, "Option 1", Toast.LENGTH_SHORT).show(); 
          return true; 
         case R.id.menu_option2: 
          Toast.makeText(MainActivity.this, "Option 2", Toast.LENGTH_SHORT).show(); 
          return true; 
         default: 
          return false; 
         } 
        } 
    }); 
    // Show the menu 
    popup.show(); 
    
    } 
    

PopupMenu將被顯示爲exa靠近您要點擊的視圖。

+0

當我點擊物品..我想禁用所有的背景像圖片.. – Asad