flutter Admob横幅广告在扑动时被切断

q3qa4bjr  于 2023-05-29  发布在  Flutter
关注(0)|答案(1)|浏览(141)

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();
  }
}

t5fffqht

t5fffqht1#

为了让它更灵敏,

**选项1:**根据维度资源文件dimens.xml中的值,使用文件夹名称对_finalSizeAdWidget大小进行数学处理配置限定符名称

values-sw720dp          10.1” tablet 1280x800 mdpi
values-sw600dp          7.0”  tablet 1024x600 mdpi
values-sw480dp          5.4”  480x854 mdpi 
values-sw480dp          5.1”  480x800 mdpi 
values-xxhdpi           5.5"  1080x1920 xxhdpi
values-xxxhdpi           5.5" 1440x2560 xxxhdpi
values-xhdpi            4.7”   1280x720 xhdpi 
values-xhdpi            4.65”  720x1280 xhdpi 
values-hdpi             4.0” 480x800 hdpi
values-hdpi             3.7” 480x854 hdpi
values-mdpi             3.2” 320x480 mdpi
values-ldpi             3.4” 240x432 ldpi
values-ldpi             3.3” 240x400 ldpi
values-ldpi             2.7” 240x320 ldpi

定义一个分数

<?xml version="1.0" encoding="utf-8"?>
 <resources>
     <fraction name="division_factor_width">0.31</fraction>
     <fraction name="division_factor_height">0.41</fraction>
 </resources>

得到分数

child: SizedBox(
       width: _finalSize!.width.toDouble() / getResources().getFraction(R.dimen.division_factor_width, 1,1),
       height: _finalSize!.height.toDouble() / getResources().getFraction(R.dimen.division_factor_height, 1,1),
       child: AdWidget(
            ad: _inlineAdaptiveAd!,
       )))

**选项2:**读取 Flutter 时向导Understanding constraints中的约束
**选项3:**将SizedBox放入另一个布局(如linear)中,并设置布局属性,使其居中而不拉伸
**选项4:**创建大小的广告,并从values.xml调用AdHelper.bannerAdUnitId,values.xml将被放置在多个大小的文件夹中,如hdpildpi ...等等

相关问题