2017-06-27 1 views
0

J'utilise un exemple de code de stackoverflow pour montrer un PopUp à la méthode OnCreate. Mon application ne se casse pas, mais elle n'affiche pas le popup. Je mets le code ci-dessous:Popup non affiché (android)

private void showExplain() { 
    LayoutInflater inflater = this.getLayoutInflater(); 
    final View layout = inflater.inflate(R.layout.popupexplicativo, null); 

    final PopupWindow windows = new PopupWindow(layout , 300,300,true); 
    windows.setFocusable(false); 
    windows.setTouchable(true); 
    windows.setOutsideTouchable(true); 
    layout.post(new Runnable() { 
     public void run() { 
      windows.showAtLocation(layout,Gravity.CENTER, 0, 0); 
     } 
    }); 
} 

Sur la méthode onCreate j'appelle showExplain();

Quelqu'un peut-il m'aider? Merci beaucoup!

Répondre

0

Essayez comme ça,

popupexplicativo.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="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#444444" 
    android:padding="10px" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:text="Transfering data" 
     android:textColor="@color/white"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:text="Status" 
     android:textColor="@color/white"/> 

    <TextView android:id="@+id/server_status_text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Awaiting answer..." 
     android:paddingLeft="10sp" 
     android:textColor="@color/white"/> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal" 
     android:gravity="center_horizontal|bottom"> 

     <Button android:id="@+id/end_data_send_button" 
      android:layout_width="100dp" 
      android:layout_height="100dp" 
      android:drawablePadding="3sp" 
      android:layout_centerHorizontal="true" 
      android:text="Cancel" /> 
    </LinearLayout> 
</LinearLayout> 

Dans votre classe java,

private void initiatePopupWindow(View v) { 
      try { 
       //We need to get the instance of the LayoutInflater, use the context of this activity 
       LayoutInflater inflater = (LayoutInflater) ProfileView.this 
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       //Inflate the view from a predefined XML layout 
       View layout = inflater.inflate(R.layout.popupexplicativo, 
         (ViewGroup) findViewById(R.id.popup_element)); 
       // create a 300px width and 470px height PopupWindow 
       pw = new PopupWindow(layout, 300, 470, true); 
       // display the popup in the center 
       pw.showAtLocation(v, Gravity.CENTER, 0, 0); 

       TextView mResultText = (TextView) layout.findViewById(R.id.server_status_text); 
       Button cancelButton = (Button) layout.findViewById(R.id.end_data_send_button); 
       cancelButton.setOnClickListener(cancel_button_click_listener); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
+0

ce qui est "popupelement"? la mise en page contextuelle? – Imrik

+0

Votre identifiant linearlayout –

+0

et quelle vue dois-je passer à cette méthode? – Imrik

0

Voilà comment je l'ai fait:

ci-dessous les méthodes sont placées dans une classe Utils (vous pouvez avoir votre propre nom de classe, c'est juste une classe utilitaire car j'ai une mise en page différente c un être gonflé dans fenêtre pop-up)

public static PopupWindow getPopupWindow(Context context, View parentLayout) { 
    final PopupWindow popUp = new PopupWindow(context); 
    popUp.setContentView(parentLayout); 
    popUp.setWidth(getScreenWidth(context)); 
    popUp.setHeight(getScreenHeight(context)); 
    popUp.setFocusable(true); 
    popUp.setOutsideTouchable(true); 

    popUp.setBackgroundDrawable(new ColorDrawable(android.R.color.transparent)); 
    return popUp; 
} 


public static int getScreenWidth(Context context) { 
    DisplayMetrics metrics = context.getResources().getDisplayMetrics(); 
    return metrics.widthPixels; 
} 

public static int getScreenHeight(Context context) { 
    //int statusBarHeight = isFullScreen() ? 0 : getStatusBarHeight(); 
    DisplayMetrics metrics = context.getResources().getDisplayMetrics(); 
    return metrics.heightPixels - getStatusBarHeight(context); 
} 


public static int getStatusBarHeight(Context context) { 
    int result = 0; 
    int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android"); 
    if (resourceId > 0) { 
     result = context.getResources().getDimensionPixelSize(resourceId); 
    } 
    return result; 
} 

et utilisé comme ceci dans votre activité

final PopupWindow popUp; 
popUp= getPopupWindow(mActivity, yourView); 
popUp.showAtLocation(anchorView, Gravity.NO_GRAVITY, 0, 0); 
popUp.update(x, y, Utils.getScreenWidth(mActivity), Utils.getScreenHeight(mActivity)); 
+0

qu'est-ce que anchorView? – Imrik

+0

J'ai aussi un problème avec la couleur transparente – Imrik

+0

et je ne peux pas résoudre le symbole "utils". – Imrik