无法让admob出现在我的surfaceView上

时间:2014-07-31 22:42:10

标签: android admob adview

我无法让admob在我的surfaceView(我称之为renderView)之上(以Z轴方式)显示。这是我用来设置布局和视图的代码。 adView的工作原理是,如果我从布局中排除了SurfaceView,它会显示在我期望的位置。否则,我看到的是我的surfaceView占据了整个屏幕。谢谢!

private void createAdNew() {

    // Create an ad.
    adView = new AdView(this);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId(AD_UNIT_ID);

    RelativeLayout.LayoutParams adViewLayoutParam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    adViewLayoutParam.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    adViewLayoutParam.addRule(RelativeLayout.CENTER_HORIZONTAL);
    adView.setLayoutParams(adViewLayoutParam);

    RelativeLayout.LayoutParams surfaceViewLayoutParam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    renderView.setLayoutParams(surfaceViewLayoutParam);

    RelativeLayout.LayoutParams parentLayoutParam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

    RelativeLayout adViewLayout = new RelativeLayout(this);
    adViewLayout.setLayoutParams(adViewLayoutParam);

    RelativeLayout parentLayout = new RelativeLayout(this);
    parentLayout.addView(renderView);
    parentLayout.addView(adView);   
    parentLayout.setLayoutParams(parentLayoutParam);

    // Create an ad request. Check logcat output for the hashed device ID to
    // get test ads on a physical device.
    AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice("6EA60118611111EB4F201E157VC99EBF")
        .build();

    // Start loading the ad in the background.
    Log.d("Ads", "Before");
    adView.loadAd(adRequest);       
    Log.d("Ads", "After");
    setContentView(parentLayout);
}

3 个答案:

答案 0 :(得分:1)

只需在AdView上设置背景颜色即可使其正常工作:

adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
adView.setBackgroundColor(Color.TRANSPARENT); 

如果您仍然遇到问题,请查看this thread

答案 1 :(得分:0)

这是我使用的一种方法:

创建将使用您喜欢的定位adView托管adView的布局

<RelativeLayout android:id="@+id/hostad"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.example.myapp"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout android:id="@+id/ad"
        android:orientation="vertical"
        android:gravity="center_horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        />
</RelativeLayout>

已经设置了ContentView

addContentView(getLayoutInflater().inflate(R.layout.ad, null), new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

然后将adView添加到布局

LinearLayout layout = (LinearLayout) findViewById(R.id.ad);
layout.addView(adView);

答案 2 :(得分:0)

您的问题是您已经为Match_PARENT,MATCH_PARENT提供了RenderView布局参数。也就是说,你已经告诉它,它的父母的所有空间都没有为AdView留下任何空间。

如果您打算继续使用RelativeLayout,则需要先配置AdView并告知RenderView位于AdView的上方/下方。

或者您可以使用LinearLayout并提供RenderView layout_gravity="1",这将告诉它使用AdView尚未使用的所有空间。

注意我强烈建议您以XML格式而不是以编程方式定义布局。这样就可以更加简单快捷地解决。