Make AdView have a fixed size upon start of an activity

我是研究僧i 提交于 2019-12-11 16:21:49

问题


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.

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>

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


回答1:


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:

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);
}

}

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>

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




回答2:


May be setting height and width dynamically on inflation can solve your problem.

 adView.setLayoutParams(new LinearLayout.LayoutParams(width, height)); //Change LinearLayout to something else if your adview is in another layout 


来源:https://stackoverflow.com/questions/20487691/make-adview-have-a-fixed-size-upon-start-of-an-activity

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!