2013-11-27 52 views
0

我有一个单独的弹出式窗口,当我在活动中调用它时效果很好。但我的想法是为这个popupWindows设置特定的类并通过不同的活动调用它。这怎么可能 ?如何在2个活动之间共享popwindows

我popupWindows类

public class GestionCat extends PopupWindow 

{ 语境m_context;

public GestionCat(Context context) 
{ 
    super(context); 

    m_context = context; 

    setContentView(LayoutInflater.from(context). 
     inflate(R.layout.cat, null)); 

    setHeight(WindowManager.LayoutParams.WRAP_CONTENT); 
    setWidth(WindowManager.LayoutParams.WRAP_CONTENT); 
} 

public void show(View anchor) 
{ 
    showAtLocation(anchor, Gravity.CENTER, 0, 0); 
} 

}

我怎么称呼它:

Activity activity = this.getParent(); 
View view = activity.findViewById(R.layout.main_layout); 
Context context = getApplicationContext(); 
GestionCat gestionCat = new GestionCat(context); 
gestionCat.show(view); 

感谢帮助

回答

0

你所寻找的是一个单身的创建。在你GestionCat类,你希望下面的代码:

private GestionCat _gestionCat; 

public static GestionCat getInstance() 
{ 
    if(_gestionCat == null) 
    { 
     _gestionCat = new GestionCat(); 
    } 

    return _gestionCat; 
} 

现在你可以使用每次GestionCat.getInstance()获得您正在寻找的GestionCat的同一个实例。这样你可以在多个类中共享弹出窗口。

更多信息请参见http://msdn.microsoft.com/en-us/library/ff650316.aspx

相关问题