0

有没有办法强制 AdView 在活动开始时立即采用其初始大小并保持在该固定大小。就像现在一样,它会在加载广告时发生变化,这意味着我的渲染器 onsurfaceChanged 函数将在启动活动时被调用两次,这会导致一些初始化运行 twize。

我可以解决这个问题,但是如果有一种简单的方法可以为通货膨胀的横幅分配固定大小,那将非常容易。我不希望对横幅尺寸的倾角值进行硬编码。我的 AdView 由以下 xml 初始化:

    <com.google.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="unit_id"
        ads:loadAdOnCreate="true"
        ads:testDevices="device_id"
        android:visibility="visible" >
    </com.google.ads.AdView>

设备/单元 ID 明显改变以保护无辜者(我)

4

2 回答 2

1

经过一番折腾后找到了自己的答案。它避免了硬编码的值,让我纯粹在 xml 中创建 AdView。以下为感兴趣的人提供:

我创建了 AdView 的这个子类:

public class MyAdView extends AdView {


public MyAdView(Activity activity, AdSize adSize, String adUnitId) {
    super(activity, adSize, adUnitId);
    // TODO Auto-generated constructor stub
}

public MyAdView(Activity activity, AdSize[] adSizes, String adUnitId) {
    super(activity, adSizes, adUnitId);
    // TODO Auto-generated constructor stub
}

public MyAdView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
}

public MyAdView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}
@Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec){
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    Context ctx=getContext();
    AdSize testSize=AdSize.createAdSize(AdSize.SMART_BANNER, ctx); //Need to do this because the final static variable SMART_BANNER has buggy behaviour and returns a negative size if you don't.
    int height=testSize.getHeightInPixels(ctx);
    int width=testSize.getWidthInPixels(ctx);
    setMeasuredDimension(width, height);
}

}

并将 xml 视图更改为(ids 再次更改):

    <packagename.MyAdView
        android:id="@id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="unitId"
        ads:loadAdOnCreate="true"
        ads:testDevices="deviceId"
        android:visibility="visible" >
    </packagename.MyAdView>

在我的布局中包含这个,我现在得到了我的 AdView 的固定大小,没有任何代码。

于 2013-12-10T08:27:11.260 回答
0

可能在通货膨胀时动态设置高度和宽度可以解决您的问题。

 adView.setLayoutParams(new LinearLayout.LayoutParams(width, height)); //Change LinearLayout to something else if your adview is in another layout 
于 2013-12-10T06:44:04.007 回答