How do I put an admob adview in the settings screen for a live wallpaper?

后端 未结 6 1840
孤街浪徒
孤街浪徒 2020-12-12 23:21

I\'ve seen the Mario live wallpaper uses admob ads in the settings screen, but I haven\'t been able to do it myself. If I put the adview into the settings.xml like you would

6条回答
  •  北荒
    北荒 (楼主)
    2020-12-12 23:47

    I've managed to answer this for myself so I'll post the solution in here in case someone else has the same question.

    I added a TabActivity as well as the standard Preferences activity, then nested the Preferences inside a Tab of the TabActivity. This means I've got a normal layout xml for the TabActivity that I can put an adview (or any other type of view) inside and I've still got the generated preferences screen working within that.

    Code for the TabActivity

    public class SettingsTabActivity extends TabActivity {
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.tablayout);
    
            TabHost tabHost = getTabHost();  // The activity TabHost
            TabHost.TabSpec spec;  // Resusable TabSpec for each tab
            Intent intent;  // Reusable Intent for each tab
    
            // Create an Intent for the regular live wallpaper preferences activity
            intent = new Intent().setClass(this, Preferences.class);
    
            // Initialize a TabSpec and set the intent
            spec = tabHost.newTabSpec("TabTitle").setContent(intent);
            spec.setIndicator("TabTitle");
    
            tabHost.addTab(spec);
    
            tabHost.setCurrentTab(0);      
        }
    }
    

    Code for the tablayout.xml

    
    
    
    
        
    
                
    
                
    
                
        
    
    

    Setting android:visibility="invisible" and android:layout_height="1dp" on the TabWidget tag means the user can't tell it's actually a Tab

提交回复
热议问题