Java源码示例:net.youmi.android.banner.AdView
示例1
private void loadYMAds() {
// 实例化 LayoutParams(重要)
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams( FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
// 设置广告条的悬浮位置
layoutParams.gravity = Gravity.BOTTOM | Gravity.RIGHT; // 这里示例为右下角
// 实例化广告条
AdView adView = new AdView(this, AdSize.FIT_SCREEN);
adView.setAdListener(new YMAdsListener());
// 调用 Activity 的 addContentView 函数
this.addContentView(adView, layoutParams);
}
示例2
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.weather_layout);
// 初始化各控件
weatherInfoLayout = (LinearLayout) findViewById(R.id.weather_info_layout);
cityNameText = (TextView) findViewById(R.id.city_name);
publishText = (TextView) findViewById(R.id.publish_text);
weatherDespText = (TextView) findViewById(R.id.weather_desp);
temp1Text = (TextView) findViewById(R.id.temp1);
temp2Text = (TextView) findViewById(R.id.temp2);
currentDateText = (TextView) findViewById(R.id.current_date);
switchCity = (Button) findViewById(R.id.switch_city);
refreshWeather = (Button) findViewById(R.id.refresh_weather);
String countyCode = getIntent().getStringExtra("county_code");
if (!TextUtils.isEmpty(countyCode)) {
// 有县级代号时就去查询天气
publishText.setText("同步中...");
weatherInfoLayout.setVisibility(View.INVISIBLE);
cityNameText.setVisibility(View.INVISIBLE);
queryWeatherCode(countyCode);
} else {
// 没有县级代号时就直接显示本地天气
showWeather();
}
switchCity.setOnClickListener(this);
refreshWeather.setOnClickListener(this);
//实例化广告条
AdView adView = new AdView(this, AdSize.FIT_SCREEN);
//获取要嵌入广告条的布局
LinearLayout adLayout=(LinearLayout)findViewById(R.id.adLayout);
//将广告条加入到布局中
adLayout.addView(adView);
}
示例3
@Override
public void onReceivedAd(AdView adView) {
// 切换广告并展示
}
示例4
@Override
public void onSwitchedAd(AdView adView) {
// 请求广告成功
}
示例5
@Override
public void onFailedToReceivedAd(AdView adView) {
// 请求广告失败
}