2016-02-09 1 views
0

Je suis en train de mettre en œuvre la screen.Here suivante sur cliquant sur l'icône de pièce jointe, fenêtre est affichée:Impossible de gérer Popup Window dans Android

Screenshot

Je suis en train de mettre en œuvre l'écran en utilisant le Mentionnés ci-dessous Code:

1.popupwindow_attachment.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/popup_element" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:padding="@dimen/padding10" 
    android:background="@color/while_color"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <TextView 
      android:id="@+id/gallery" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:drawablePadding="@dimen/padding10" 
      android:drawableTop="@drawable/gallery" 
      android:gravity="center" 
      android:text="Gallery" /> 

     <TextView 
      android:id="@+id/photos" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:drawablePadding="@dimen/padding10" 
      android:drawableTop="@drawable/photos" 
      android:gravity="center" 
      android:text="Photos" /> 

     <TextView 
      android:id="@+id/videos" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:drawablePadding="@dimen/padding10" 
      android:drawableTop="@drawable/videos" 
      android:gravity="center" 
      android:text="Gallery" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <TextView 
      android:id="@+id/audio" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:drawablePadding="@dimen/padding10" 
      android:drawableTop="@drawable/audio" 
      android:gravity="center" 
      android:text="Audio" /> 

     <TextView 
      android:id="@+id/location" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:drawablePadding="@dimen/padding10" 
      android:drawableTop="@drawable/location" 
      android:gravity="center" 
      android:text="Location" /> 

     <TextView 
      android:id="@+id/contacts" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:drawablePadding="@dimen/padding10" 
      android:drawableTop="@drawable/contacts" 
      android:gravity="center" 
      android:text="Contacts" /> 
    </LinearLayout> 

</LinearLayout> 

2.Code mettre en œuvre la fenêtre

private void initializePopUpWindow() { 

     //inflate the popupwindow_attachment.xml 
     LinearLayout viewGroup = (LinearLayout) SingleChatActivity.this.findViewById(R.id.popup_element); 
     LayoutInflater inflater = (LayoutInflater) SingleChatActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View layout = inflater.inflate(R.layout.popupwindow_attachment, viewGroup); 
     popupWindow = new PopupWindow(layout, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT,true); 

     //Displaying the popup at a specific location 
     popupWindow.showAtLocation(layout,Gravity.CENTER,0,0); 

    } 

Je reçois l'écran suivant après avoir utilisé ce code:

Screenshot

Comme vous pouvez le voir ici, la fenêtre pop-up est pas en dessous de la barre d'outils. Aidez-moi s'il vous plaît à résoudre le problème.

Modifié le code:

private void initializePopUpWindow() { 

    //inflate the popupwindow_attachment.xml 
    LinearLayout viewGroup = (LinearLayout) SingleChatActivity.this.findViewById(R.id.popup_element); 
    LayoutInflater inflater = (LayoutInflater) SingleChatActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View layout = inflater.inflate(R.layout.popupwindow_attachment, viewGroup); 
    popupWindow = new PopupWindow(layout, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, true); 

    //Displaying the popup at a specific location 
    // popupWindow.showAtLocation(layout, Gravity.TOP, 0, 150); 
    popupWindow.showAsDropDown(toolbar,0,0); 

    //Close the popup when touch outside 
    popupWindow.setOutsideTouchable(true); 
    popupWindow.setFocusable(true); 
    // popupWindow.dismiss(); 

} 

Après avoir utilisé le code ci-dessus, la fenêtre pop-up est en dessous de la barre d'outils que je veux mais pas fermé après avoir cliqué sur l'extérieur fonctionnalité it.No travaille dans le screen.Screen est juste pendu mal.

Modifié Code du travail:

méthode 1.onCreate()

protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_single_chat); 

      //Toolbar 
      toolbar = (Toolbar) findViewById(R.id.toolbarSingleChat); 
      toolbar.setNavigationIcon(R.drawable.back); // Setting Navigation Icon in the Toolbar 
      setSupportActionBar(toolbar); 

    LinearLayout viewGroup = (LinearLayout) SingleChatActivity.this.findViewById(R.id.popup_element); 
      LayoutInflater inflater = (LayoutInflater) SingleChatActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View layout = inflater.inflate(R.layout.popupwindow_attachment, viewGroup); 
      popupWindow = new PopupWindow(layout, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT); 

      //Close the popup when touch outside 
      popupWindow.setOutsideTouchable(true); 
      popupWindow.setFocusable(true); 
      popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 
    } 

2.onoptionsItemSelected()

public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case R.id.action_viewContacts: 
       return true; 
      case R.id.action_media: 
       return true; 
      case R.id.action_search: 
       return true; 
      case R.id.action_block: 
       return true; 
      case R.id.action_email_chat: 
       return true; 
      case R.id.action_clear_chat: 
       return true; 
      case R.id.action_attach: 
       initializePopUpWindow(); 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 

méthode 3.initializePopUpWindow() :

private void initializePopUpWindow() { 
     popupWindow.showAsDropDown(toolbar, 0, 0); 
    } 

Maintenant, cela fonctionne pour moi.

Répondre

2

Vous pouvez faire somthing comme ceci:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    textView = (TextView)findViewById(R.id.text); 
    textView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      showpopUp(); 
     } 
    }); 

} 

public void showpopUp() 
{ 
    LayoutInflater layoutInflater 
      = (LayoutInflater)getBaseContext() 
      .getSystemService(LAYOUT_INFLATER_SERVICE); 
    View popupView = layoutInflater.inflate(R.layout.popup, null); 
    final PopupWindow popupWindow = new PopupWindow(
      popupView, 
      RelativeLayout.LayoutParams.WRAP_CONTENT, 
      RelativeLayout.LayoutParams.WRAP_CONTENT); 

popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 
    popupWindow.setOutsideTouchable(true); 
    popupWindow.setFocusable(true); 

    popupWindow.showAsDropDown(textView, 0, 0); 
} 
+0

Mais il affichera la fenêtre pop-up sous la vue cliquée. Je veux une fenêtre pop-up en dessous d'une vue spécifique. Dans mon cas, je le veux en dessous de la barre d'outils qui contient une action de pièce jointe. –

+0

exactement de sorte que vous pouvez passer ici vue de la barre d'outils de sorte qu'il sera affiché ci-dessous barre d'outils. Qu'est-ce que tu veux d'autre? – justDroid

+0

: S'il vous plaît vérifier mon code édité. Fenêtre Popup est ajouté avec succès sous la barre d'outils. Mais il n'est pas supprimé après avoir cliqué en dehors de lui. L'écran est juste pendu. Aucune fonctionnalité ne fonctionne. –

3

Si vous voulez que votre pop-up en haut, alors vous devez définir Gravity.TOP pas Gravity.CENTER

Remplacer

popupWindow.showAtLocation(layout,Gravity.CENTER,0,0); 

avec

popupWindow.showAtLocation(layout,Gravity.TOP,0,0); 

Mise à jour

Si vous Voulez-vous la fenêtre ci-dessous la barre d'outils, alors vous devriez faire quelque chose comme

popupWindow.showAtLocation(layout,Gravity.TOP,0,100);// where 100 is the height of toolbar 
+0

J'ai essayé ce .Il me montre la popupWindow en haut de l'écran, mais Je le veux en dessous de la barre d'outils. popupWindow.showAtLocation (layout, Gravity.TOP, 0,150); Cela fonctionne pour moi sur mon Yureka mbile. J'ai besoin de vérifier si cela fonctionnera sur les appareils ou non. –

+0

au lieu de la mise en page, passez la barre d'outils dans popupWindow.showAtLocation (layout, Gravity.TOP, 0,0); – justDroid

+0

@justDroid Pouvez-vous expliquer en détail avec un exemple? –