问题
OK, I wanna know if this can be done..
All I need is a common footer like bar, which will contain ads that are going to be displayed. I was wondering if there was any way by which I can have this part of my application as a common one.
I am aware of the include tag, but all that does is to add a that particular layout anywhere it is referenced. What that does is it prompts a reload of the ad, everytime I move from one activity to another. This is very annoying as there is a new ad request that is being sent everytime I move to a new activity.
I am using admob to display ads. Hope I have made the question clear.
回答1:
Create your view programatically then store it in a singleton object which can be accessed throughout the application, this will avoid having to create a new View each time.
Singleton class example:
public class MySingleFooter
{
MySingleFooter mySingleFooter;
View myFooter;
private MySingleFooter()
{
}
public static MySingleFooter getInstance()
{
if (mySingleFooter == null)
{
mySingleFooter = new MySingleFooter();
}
return mySingleFooter;
}
void setFooter(View myFooter)
{
this.myFooter = myFooter;
}
View getFooter()
{
return myFooter;
}
}
You can set the footer from all activities like this:
MySingleFooter footerStore = MySingleFooter.getInstance();
footerStore.setFooter(thefooter);
You can retrieve the footer from all activities like this:
MySingleFooter footerStore = MySingleFooter.getInstance();
View myview = footerStore.getFooter(thefooter);
Then add it to the current activity programatically.
来源:https://stackoverflow.com/questions/5408336/a-common-footer-for-an-entire-application-android