Android create reusable layout

2019-09-19 15:41发布

问题:

How to create a single pop up layout with textbox that can be called anywhere on the app. See below pseudo codes of what I mean;

PopUpClass popup;
private static Singleton instance = null;
class Singleton {
   if(instance == null) {
      instance = new Singleton ();
      // Instantiate a class pop up class layout
       popup = new PopUpClass(); // I dont know how to create this one
    }
    return instance;
}


SomeClass {
   private buttonClicked {
   Singleton singleton = new Singleton();
   PopUpClass popup = singleton.popup;

   //add the pop up class to current view
   }
}

This means that I want the pop up class created only once so that whatever text on the layout that done on someclass it can also be show on other class.

回答1:

If you want to show same textbox or other widgets in every layout, then better way is to create a layout and include this layout in every activity you want to show this.. Can refer this linklink