自适应横幅广告
自适应横幅广告的类型
Yandex Mobile Ads SDK 提供两种类型的自适应横幅广告:
|
横幅广告 |
描述 |
使用场景 |
|
粘性横幅广告 |
固定在屏幕边缘并显示在其他元素之上的横幅广告。此类横幅广告不受屏幕滚动影响。 |
如果您希望横幅广告保持“固定效果”,即保持位置不变,即使用户滚动浏览屏幕也始终显示在内容上方。 |
|
内嵌横幅广告 |
嵌入应用中(例如,嵌入内容或列表中)的横幅广告,如同用户界面元素一样呈现。 |
横幅广告应成为页面内容的一部分,例如出现在动态消息中、帖子之间或作为列表项显示。 |
所有类型的横幅广告都会每隔 60 秒自动更新一次广告创意。
提示
要设置自定义自动更新周期,请联系您的专属经理。
从代码角度来看,这些横幅广告仅在尺寸设置方式上有所不同。例如:
粘性横幅广告
内嵌横幅广告
BannerAdSize.sticky({required int width})
BannerAdSize.inline({required int width, required int maxHeight})
|
实体 |
描述 |
|
|
横幅广告高度:
所有横幅广告尺寸必须以 dp 为单位指定。 |
|
|
使用:
|
备注
演示项目中提供了展示所有格式类型运作原理的示例。
自适应横幅广告创建示例
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:yandex_mobileads/mobile_ads.dart';
class BannerPage extends StatefulWidget {
const BannerPage({super.key});
@override
State<BannerPage> createState() => _BannerPageState();
}
class _BannerPageState extends State<BannerPage> {
static const _tag = 'Banner';
static const _adUnitId = 'demo-banner-yandex';
BannerAd? _banner;
bool _adLoaded = false;
String _status = 'Loading...';
@override
void didChangeDependencies() {
super.didChangeDependencies();
if (_banner == null) {
_createBanner();
}
}
@override
void dispose() {
_banner?.destroy();
super.dispose();
}
void _createBanner() {
final size = MediaQuery.of(context).size;
_banner = BannerAd(
adUnitId: _adUnitId,
adSize: BannerAdSize.inline(
width: size.width.round(),
maxHeight: (size.height / 3).round(),
),
adRequest: const AdRequest(),
onAdLoaded: () {
debugPrint('[$_tag] onAdLoaded');
if (mounted) setState(() { _adLoaded = true; _status = 'Loaded'; });
},
onAdFailedToLoad: (error) {
debugPrint('[$_tag] onAdFailedToLoad: ${error.description}');
if (mounted) setState(() => _status = 'Error: ${error.description}');
},
onAdClicked: () => debugPrint('[$_tag] onAdClicked'),
onLeftApplication: () => debugPrint('[$_tag] onLeftApplication'),
onReturnedToApplication: () => debugPrint('[$_tag] onReturnedToApplication'),
onImpression: (ImpressionData impressionData) => debugPrint('[$_tag] onAdImpression: $impressionData'),
onAdClose: () => debugPrint('[$_tag] onAdClose'),
);
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Banner')),
body: Column(
children: [
Padding(
padding: const EdgeInsets.all(16),
child: Text(_status),
),
const Spacer(),
if (_banner != null)
SizedBox(
width: double.infinity,
child: AdWidget(bannerAd: _banner!),
),
],
),
);
}
}
检查集成
创建并运行您的项目。您可以通过在 Android Studio 的 Logcat 中搜索 YandexAds 关键字来检查集成是否成功:
[Integration] Ad type Banner was integrated successfully