【问题标题】:Make AdView have a fixed size upon start of an activity在活动开始时使 AdView 具有固定大小
【发布时间】:2013-12-10 06:31:05
【问题描述】:

有没有办法强制 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

【问题讨论】:

    标签: android xml adview


    【解决方案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 获得一个固定大小,而无需任何代码。

    【讨论】:

    • AdView 现在是最后一课了
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2013-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-06
      • 2010-12-23
      • 2021-05-10
      • 2011-02-28
      相关资源
      最近更新 更多