일부 엉성한 답변을 찾았습니다. 하드 코드 된 값을 피하고 순수하게 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의 고정 크기를 얻을 수 있습니다.