2015-01-28 2 views
0

Я создаю popupWindow, но он не отображается на моем Fragment, когда я его вызываю.
Вот мой код popupWindow:popup WIndow не отображается

LayoutInflater layoutInflater = 
      (LayoutInflater)getActivity() 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View popupView = layoutInflater.inflate(R.layout.popup, null); 
    final PopupWindow popupWindow = new PopupWindow(
      popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 


    Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss); 
    btnDismiss.setOnClickListener(new Button.OnClickListener(){ 

     @Override 
     public void onClick(View v) { 
      popupWindow.dismiss(); 
     }}); 

    popupWindow.showAsDropDown(getView()); 
    //popupWindow.showAsDropDown(btnOpenPopup, 50, -30); 
    //popupWindow.showAsDropDown(getCurrentFocus()); 
    popupView.setOnTouchListener(new OnTouchListener() { 
     int orgX, orgY; 
     int offsetX, offsetY; 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       orgX = (int) event.getX(); 
       orgY = (int) event.getY(); 
       break; 
      case MotionEvent.ACTION_MOVE: 
       offsetX = (int)event.getRawX() - orgX; 
       offsetY = (int)event.getRawY() - orgY; 
       popupWindow.update(offsetX, offsetY, -1, -1, true); 
       break; 
      } 
      return true; 
     }}); 
+0

Вызвать метод show для объекта popupwindow. –

+0

ну его внутри public void popups(), я вызываю, что всплывающие окна на фрагменте все еще не появляются сэр – Mary

ответ

2

Используйте следующий код, чтобы показать Pop Up window.It будет работать для вас.

View popupView = layoutInflater.inflate(R.layout.popup, null); 
    final PopupWindow popupWindow = new PopupWindow(
      popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
popupWindow.showAtLocation(popupView , Gravity.CENTER, 0, 0); 
3

Приведенный ниже код может зависеть от вашей спецификации. Вызов этого метода внутри OnClick (View В) OnClickListener присвоенного Вид:

public void showPopup(View anchorView) { 

    View popupView = getLayoutInflater().inflate(R.layout.popup_layout, null); 

    PopupWindow popupWindow = new PopupWindow(popupView, 
          LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

    // Example: If you have a TextView inside `popup_layout.xml`  
    TextView tv = (TextView) popupView.findViewById(R.id.tv); 

    tv.setText(....); 

    // Initialize more widgets from `popup_layout.xml` 
    .... 
    .... 

    // If the PopupWindow should be focusable 
    popupWindow.setFocusable(true); 

    // If you need the PopupWindow to dismiss when when touched outside 
    popupWindow.setBackgroundDrawable(new ColorDrawable()); 

    int location[] = new int[2]; 

    // Get the View's(the one that was clicked in the Fragment) location 
    anchorView.getLocationOnScreen(location); 

    // Using location, the PopupWindow will be displayed right under anchorView 
    popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY, 
            location[0], location[1] + anchorView.getHeight()); 

} 

здесь anchorView является v из OnClick (View v).

1
final PopupWindow popupWindow = new PopupWindow(
     popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

Проверьте, что используемые LayoutParams сопоставляются с родителем представления. , например. используйте, если родительский элемент LinearLayout, используйте LinearLayout.LayoutParams.WRAP_CONTENT

1

Вот пример кода для отображения и скрытия всплывающего окна.

TextView popupWindowTextView = new TextView(getActivity()); 
    Button popupWindowButton = new Button(getActivity()); 
    LinearLayout layout = new LinearLayout(getActivity()); 
    popupWindowButton.setText("OK"); 
    popupWindowTextView.setText("Popup Window. Click on OK to dismiss."); 
    popupWindowTextView.setPadding(0, 0, 0, 20); 
    layout.setOrientation(1); 
    layout.addView(popupWindowTextView); 
    layout.addView(popupWindowButton); 

    int popupWindowWidth = 200; 
    int popupWindowHeight = 150; 
    final PopupWindow popupWindow = new PopupWindow(context); 
    popupWindow.setContentView(layout); 
    popupWindow.setWidth(popupWidth); 
    popupWindow.setHeight(popupHeight); 
    popupWindow.setFocusable(true); 
    popupWindow.showAtLocation(layout, Gravity.NO_GRAVITY, popupWindowWidth, popupWindowHeight); 
    popupWindowButton.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      popupWindow.dismiss(); 
     } 
    });