大家好,我在我的一个应用程序中使用 MMAdView,它在除 SAMSUNG S3 之外的所有设备中都运行良好,为什么会发生这种情况,对这个问题有任何想法。
这里附有屏幕短片
这个 480x800 Htc 设备
这里是三星 s3 设备 720x1280
IN 三星 s3 设备右侧角落的空白区域。
这是我正在使用的代码。
活动代码
public class MainActivity extends Activity {
private static final int MED_BANNER_WIDTH = 480;
private static final int MED_BANNER_HEIGHT = 60;
//Constants for phone sized ads (320x50)
private static final int BANNER_AD_WIDTH = 320;
private static final int BANNER_AD_HEIGHT = 50;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("MainActivity", "one");
int placementWidth = BANNER_AD_WIDTH;
int placementHeight = BANNER_AD_HEIGHT;
//Finds an ad that best fits a users device.
if(canFit(BANNER_AD_WIDTH)) {
Log.i("MainActivity", "two");
placementWidth = BANNER_AD_WIDTH;
placementHeight = BANNER_AD_HEIGHT;
} else if(canFit(MED_BANNER_WIDTH)) {
Log.i("MainActivity", "three");
placementWidth = MED_BANNER_WIDTH;
placementHeight = MED_BANNER_HEIGHT;
}
MMAdView adView = new MMAdView(this);
adView.setApid("Appid");
MMRequest request = new MMRequest();
adView.setMMRequest(request);
adView.setId(MMSDK.getDefaultAdId());
adView.setWidth(placementWidth);
adView.setHeight(placementHeight);
//Calculate the size of the adView based on the ad size. Replace the width and height values if needed.
int layoutWidth = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, placementWidth, getResources().getDisplayMetrics());
int layoutHeight = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, placementHeight, getResources().getDisplayMetrics());
//Create the layout parameters using the calculated adView width and height.
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(layoutWidth, layoutHeight);
//This positions the banner.
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
adView.setLayoutParams(layoutParams);
adView.getAd();
Log.i("MainActivity", "four");
RelativeLayout layout = (RelativeLayout) findViewById(R.id.adBannerRelativeLayout);
//Add the adView to the layout. The layout is assumed to be a RelativeLayout.
layout.addView(adView);
}
protected boolean canFit(int adWidth) {
Log.i("MainActivity", "five");
int adWidthPx = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
adWidth, getResources().getDisplayMetrics());
DisplayMetrics metrics = this.getResources().getDisplayMetrics();
Log.i("MainActivity", " metrics==> "+ metrics);
return metrics.widthPixels >= adWidthPx;
}
}
这是 XML 文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mm="http://millennialmedia.com/android/schema"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="@+id/adBannerRelativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</RelativeLayout>
EDIT:
这是我正在使用MAdView的文档