Android Studio新手求助:无activity_main.xml的Frames应用集成AdMob广告
Hey there! As a fellow Android dev, I get how confusing it can be when a project doesn’t follow the "standard" structure you’re used to. Let’s break down how to add AdMob ads to the Frames app step by step:
1. First, Set Up AdMob Dependencies & Manifest
First, make sure your project has the necessary AdMob setup:
- Open your app-level
build.gradle(Module: app) and add this dependency (you can check for the latest version if needed):implementation 'com.google.android.gms:play-services-ads:22.6.0' - Sync your project to apply the changes.
- Open
AndroidManifest.xmland add these lines inside the<application>tag (replace with your actual AdMob app ID):<meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/> - Double-check you have the internet permission (it’s probably already present for the wallpaper app, but better safe than sorry):
<uses-permission android:name="android.permission.INTERNET"/>
2. Locate the Main Content Container
Since there’s no activity_main.xml, the Frames app likely uses a fragment-based structure or dynamically creates its main layout in code. Let’s find where to insert the ad:
- Open
MainActivity.kt(or the main activity class) and look at theonCreatemethod. You’ll probably see code like this:supportFragmentManager.beginTransaction() .replace(R.id.container, MainFragment()) .commit() - The
R.id.containeris the layout container holding the main content. This container is defined in another layout file (maybeactivity_main_container.xmlor similar) — locate that file, or if the container is created dynamically, we’ll add the ad directly via code.
3. Add the AdView to the Layout
You have two straightforward options here:
Option A: Add via XML Layout
- Create a new layout file, e.g.,
ad_banner.xml:<?xml version="1.0" encoding="utf-8"?> <com.google.android.gms.ads.AdView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/res-auto" android:id="@+id/adView" android:layout_width="match_parent" android:layout_height="wrap_content" ads:adSize="BANNER" ads:adUnitId="ca-app-pub-3940256099942544/6300978111"/> <!-- Use test ID first! --> - Open the container layout (the one with
R.id.container) and include this ad layout where you want it (e.g., at the bottom of the screen):<include layout="@layout/ad_banner" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom"/>
Option B: Add via Code (If Layout Is Dynamic)
If the main container is created programmatically, you can instantiate the AdView directly in your activity:
val adView = AdView(this) adView.adSize = AdSize.BANNER adView.adUnitId = "ca-app-pub-3940256099942544/6300978111" // Use test ID first // Add the AdView to your main container (e.g., a FrameLayout) val container = findViewById<FrameLayout>(R.id.container) val layoutParams = FrameLayout.LayoutParams( FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT ) layoutParams.gravity = Gravity.BOTTOM container.addView(adView, layoutParams)
4. Initialize AdMob & Load the Ad
Now, initialize MobileAds and load the ad in your MainActivity (or MainFragment if the content lives there):
class MainActivity : AppCompatActivity() { private lateinit var adView: AdView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.your_container_layout) // Initialize AdMob MobileAds.initialize(this) {} // Get reference to AdView adView = findViewById(R.id.adView) // Load the ad val adRequest = AdRequest.Builder().build() adView.loadAd(adRequest) } // Handle AdView lifecycle to prevent memory leaks override fun onPause() { adView.pause() super.onPause() } override fun onResume() { super.onResume() adView.resume() } override fun onDestroy() { adView.destroy() super.onDestroy() } }
Important Notes
- Always use test ad IDs first (like the ones in the examples) to avoid getting your AdMob account suspended. Replace them with your real AdUnit IDs once you’re ready to publish.
- If the main content is in a Fragment instead of the Activity, move the AdView setup code to that Fragment’s
onViewCreatedmethod instead. - Quick check: The Frames project is open-source, so you might want to glance at its GitHub issues or README — other developers might have already shared AdMob integration tips!
内容的提问来源于stack exchange,提问作者Teja Pamanji

