AdMob Banner显示在View后面?

时间:2014-06-06 18:46:47

标签: android youtube-api admob android-framelayout

您好我正在使用AdMoB在YouTube应用中显示横幅广告,该广告从频道中检索视频。为了显示广告,我使用的是Java代码而不是XML。现在我面临一个问题,即在加载后我的横幅显示在视频后面。有人可以帮我找到一种方法来显示视频的广告。这是我使用的代码

        adView = new AdView(this, AdSize.BANNER, "caXXXXXXXXXXXX");
        FrameLayout layout = (FrameLayout)findViewById(R.id.content_frame);
        layout.addView(adView);
        @SuppressWarnings("deprecation")
        FrameLayout.LayoutParams adsParams = new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
        adView.loadAd(new AdRequest());

2 个答案:

答案 0 :(得分:0)

您使用的是什么布局?要在我的Libgdx项目之上获取广告,我必须使用RelativeLayout并指定按特定顺序放置内容。这是我的代码中的一个例子:


// Create the main/content layout
RelativeLayout layout = new RelativeLayout(this);

// This is just a View that LibGDX draws into, it could be any View.
View gameView = initializeForView(new SuperAwesomeGameIMade(AndroidLauncher.this)); 

// Add it to the new content layout.
layout.addView(gameView);

// Create and setup the AdMob View
mAdView = new AdView(AndroidLauncher.this);
mAdView.setAdSize(AdSize.SMART_BANNER);
mAdView.setAdUnitId("your ad unit here");

// Create the ad load request using the AdRequest Builder
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
adRequestBuilder.addTestDevice("your test device ID so you don't get banned!");
mAdView.setVisibility(View.GONE); // Change to View.Visible to show ads
mAdView.loadAd(adRequestBuilder.build());

// Setup the AdView with new layout params so it can "float" on the other one.
RelativeLayout.LayoutParams adParams = 
        new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                                        RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
adParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layout.addView(mAdView, adParams);

// Set the content view as this new relative layout
setContentView(layout);

希望能让你指出正确的方向。

答案 1 :(得分:0)

试试这种方式。

这是横幅的xml代码,您可以将它放在活动xml布局中的任何位置:

<com.google.ads.AdView android:id="@+id/banner"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       ads:adUnitId="YOUR UNIT ID HERE"
       ads:adSize="BANNER"
       ads:loadAdsOnCreate="true"/>

然后在<application>中的AndroidManifest.xml标记内声明了AdActivity:

<activity
        android:name="com.google.ads.AdActivity"
        android:label="@string/app_name" >

最终获得关于您的横幅活动的参考,并从谷歌开始请求广告:

AdView ad = (AdView) findViewById(R.id.banner);
ad.loadAd(new AdRequest());