是否可以在 android eclipse 中使用 java 编码生成广告横幅,而不使用任何 xml<com.google.....>
标签?
问问题
907 次
2 回答
0
您可以创建一个 AdView 对象并添加到框架布局中,例如,
AdView mAdView = new AdView(context);
mAdView.setAdSize(AdSize.BANNER);
mAdView.setAdUnitId(AD_UNIT_ID);
FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.adView);
frameLayout.addView(mAdView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
希望这有帮助!
于 2017-04-07T10:11:28.970 回答
0
将此添加到您的活动中
//Add this in OnCreate of Activity to initialize the ad
MobileAds.initialize(getApplicationContext(), "< your-ad-unit-Id >");
//Add this wherever your code needs to add the ad
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
//Additionally to adjust the position to Bottom
layout.setGravity(Gravity.BOTTOM);
// Create a banner ad
mAdView = new AdView(this);
mAdView.setAdSize(AdSize.SMART_BANNER);
mAdView.setAdUnitId("<your-ad-unit-Id>");
// Create an ad request.
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
// Optionally populate the ad request builder.
adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
// Add the AdView to the view hierarchy.
layout.addView(mAdView);
// Start loading the ad.
mAdView.loadAd(adRequestBuilder.build());
setContentView(layout);
然后检查您是否已在Manifest中授予权限
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
在Manifest的 <application> 里面添加下面的元数据
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
最后检查您是否添加了在app gradle中编译所需的依赖项
compile 'com.google.android.gms:play-services:9.6.1'
于 2017-04-07T12:15:29.313 回答