使AD浏览时的活性的启动有一个固定的大小活性、有一个、大小、AD

2023-09-04 03:18:43 作者:小奶猫

有没有强迫AD浏览报把它的方法的最初规模后立即活动的开始,留在那个固定的大小。因为它是现在,它在加载广告的变化,这意味着我的渲染器在启动活动,这将导致一些初始化要运行twize onsurfaceChanged函数将被调用两次。

Is there a way of forcing AdView to take it's initial size immediately upon activity start and stay at that fixed size. As it is now, it changes upon loading of the ads, meaning my renderers onsurfaceChanged function will be called twice upon starting the activity, which causes some initialization to be run twize.

我可以解决这一问题,但它是非常非常容易,如果有一个简单的方法来固定的大小分配给对通胀的旗帜。我想preFER不为旗帜大小的硬盘code浸值。我的AD浏览报是由以下XML初始化:

I can work around this, but it would be very very much easier if there was a simple way to assign a fixed size to the banner on inflation. I would prefer not to hardcode dip values for the banner size. My AdView is initialized by the below 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>

设备/装置IDS明显变化,以保护无辜(我)

Device/unit ids obviously changed to protect the innocent (me)

推荐答案

后,找到一些瞎我自己的答案。它避免了硬codeD值,然后让我在纯粹的XML创建AD浏览报。以下提供,有兴趣的:

Found my own answer after some mucking about. It avoids the hardcoded values and lets me create the AdView purely in xml. Provided below for anyone interested:

我创造了这个子类的AD浏览:

I created this subclass of 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再次改变):

And changed the xml view to (ids changed again):

    <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>

包括在我的布局,我现在得到一个固定大小为我的AD浏览报,没有任何code。

Including this in my layout, I now get a fixed size for my AdView, without any code.