-1
是否可以在android eclipse中使用java編碼生成廣告橫幅,而不使用任何xml <com.google.....>
標籤?Android:如何僅使用Java而不是xml來顯示廣告
是否可以在android eclipse中使用java編碼生成廣告橫幅,而不使用任何xml <com.google.....>
標籤?Android:如何僅使用Java而不是xml來顯示廣告
您可以創建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);
希望這有助於!
在活動添加此
//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);
然後檢查是否已在所需的權限清單
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
在添加以下里面<應用>元數據清單
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
最後檢查,如果你已經添加所需依賴在應用gradle這個
compile 'com.google.android.gms:play-services:9.6.1'
是以編程方式創建視圖並將視圖分配給廣告 – Prateekro
你有任何源代碼 –
http://stackoverflow.com/a/24411058/2714340檢查此創建視圖 – Prateekro