如何在基于ZXing的扫码Camera Activity中添加AdMob横幅广告?
我完全懂你的困扰——用ZXing的ZXingScannerView时,很多人习惯直接在代码里把ScannerView设为Activity的内容视图,跳过了布局XML,这就导致没法直接插入广告控件。其实只要稍微调整下结构,用布局容器把扫码视图和广告视图组合起来就行,具体步骤如下:
1. 创建包含扫码视图和广告的布局XML
首先在res/layout下新建activity_main.xml,用ConstraintLayout或者RelativeLayout来布局,让扫码视图占满屏幕主体,广告固定在底部。这里用ConstraintLayout举例:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- ZXing扫码视图,占满除底部广告外的所有区域 --> <me.dm7.barcodescanner.zxing.ZXingScannerView android:id="@+id/scanner_view" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toTopOf="@id/ad_view" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"/> <!-- AdMob横幅广告,固定在屏幕底部 --> <com.google.android.gms.ads.AdView android:id="@+id/ad_view" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:adSize="BANNER" app:adUnitId="你的广告单元ID"/> </androidx.constraintlayout.widget.ConstraintLayout>
注意替换
app:adUnitId里的内容为你自己的AdMob横幅广告单元ID,同时记得在AndroidManifest.xml的<application>标签下配置AdMob的App ID:<meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="你的App ID"/>。
2. 修改MainActivity代码,加载布局并初始化控件
原来的代码应该是直接setContentView(new ZXingScannerView(this)),现在改成加载上面的自定义布局,然后分别获取扫码视图和广告视图的实例并初始化:
public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler { private ZXingScannerView mScannerView; private AdView mAdView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 加载自定义布局,替代直接创建ScannerView作为根视图 setContentView(R.layout.activity_main); // 获取扫码视图实例 mScannerView = findViewById(R.id.scanner_view); // 初始化AdMob广告 mAdView = findViewById(R.id.ad_view); AdRequest adRequest = new AdRequest.Builder().build(); mAdView.loadAd(adRequest); } @Override public void onResume() { super.onResume(); // 恢复扫码功能 mScannerView.setResultHandler(this); mScannerView.startCamera(); } @Override public void onPause() { super.onPause(); // 暂停扫码和广告加载,避免资源浪费 mScannerView.stopCamera(); if (mAdView != null) { mAdView.pause(); } } @Override public void onDestroy() { super.onDestroy(); // 销毁广告视图,释放资源 if (mAdView != null) { mAdView.destroy(); } } @Override public void handleResult(com.google.zxing.Result result) { // 这里写你的扫码结果处理逻辑,比如显示结果、跳转页面等 Toast.makeText(this, "扫描结果: " + result.getText(), Toast.LENGTH_SHORT).show(); // 如果需要继续扫码,调用下面的方法恢复预览 mScannerView.resumeCameraPreview(this); } }
3. 补充必要的权限配置
确保AndroidManifest.xml里已经添加了相机和AdMob所需的权限:
<uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
这样调整后,扫码视图会占满除底部广告外的所有屏幕区域,广告会稳定固定在底部显示。核心思路就是用布局容器把扫码视图和广告视图组合起来,而不是直接把ScannerView作为Activity的根视图。
内容的提问来源于stack exchange,提问作者cardaguh

