bounty将在5天内到期。回答此问题可获得+100声望奖励。Patola正在寻找一个答案从一个有信誉的来源。
如图所示,广告横幅广告被切断或拉伸。
如果我使用AdSize。横幅小部件不采取整个宽度,但显示广告正确。如果我使用全横幅广告是拉伸。
如何让横幅适合屏幕的整个宽度并正确显示内容(没有切断或条纹)?
class _AdBannerState extends State<AdBanner> {
BannerAd? _inlineAdaptiveAd;
bool _isLoaded = false;
AdSize? _finalSize;
@override
void didChangeDependencies() {
super.didChangeDependencies();
_loadAd();
}
void _loadAd() async {
await _inlineAdaptiveAd?.dispose();
setState(() {
_inlineAdaptiveAd = null;
_isLoaded = false;
});
// Get an inline adaptive size for the current orientation.
AdSize size = AdSize.fullBanner;
_inlineAdaptiveAd = BannerAd(
adUnitId: AdHelper.bannerAdUnitId,
size: size,
request: const AdRequest(),
listener: BannerAdListener(
onAdLoaded: (Ad ad) async {
debugPrint('Inline adaptive banner loaded: ${ad.responseInfo}');
// After the ad is loaded, get the platform ad size and use it to
// update the height of the container. This is necessary because the
// height can change after the ad is loaded.
BannerAd bannerAd = (ad as BannerAd);
_finalSize = await bannerAd.getPlatformAdSize();
// if (size == null) {
// debugPrint(
// 'Error: getPlatformAdSize() returned null for $bannerAd');
// return;
// }
setState(() {
_inlineAdaptiveAd = bannerAd;
_isLoaded = true;
});
},
onAdFailedToLoad: (Ad ad, LoadAdError error) {
debugPrint('Inline adaptive banner failedToLoad: $error');
ad.dispose();
},
),
);
await _inlineAdaptiveAd!.load();
}
@override
Widget build(BuildContext context) {
return (_inlineAdaptiveAd != null && _isLoaded && _finalSize != null)
? Expanded(
child: SizedBox(
width: _finalSize!.width.toDouble(),
height: _finalSize!.height.toDouble(),
child: AdWidget(
ad: _inlineAdaptiveAd!,
)))
: const SizedBox(
width: 0,
height: 0,
);
}
@override
void dispose() {
_inlineAdaptiveAd?.dispose();
super.dispose();
}
}
1条答案
按热度按时间t5fffqht1#
为了让它更灵敏,
**选项1:**根据维度资源文件
dimens.xml
中的值,使用文件夹名称对_finalSize
和AdWidget
大小进行数学处理配置限定符名称定义一个分数
得到分数
**选项2:**读取 Flutter 时向导Understanding constraints中的约束
**选项3:**将SizedBox放入另一个布局(如linear)中,并设置布局属性,使其居中而不拉伸
**选项4:**创建大小的广告,并从
values.xml
调用AdHelper.bannerAdUnitId
,values.xml将被放置在多个大小的文件夹中,如hdpi
ldpi
...等等