如何在startCamera()方法之后(使用xzing库扫描QR码)将AdMob横幅添加到相机活动?

时间:2018-04-23 10:56:54

标签: java android camera admob

我在com.google.zxing.Result和me.dm7.barcodescanner.zxing.ZXingScannerView包中使用了一个名为zxing的库来扫描我的新应用中的QR码,但是我无法在其中放置Admob横幅扫描QR码的Camera Activity的底部,我不知道如何让它出现在Camera Acvitity中,因为看起来相机活动没有布局XML来放置admob XML代码。

public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {

private ZXingScannerView mScannerView;
MarshMallowPermission marshMallowPermission = new MarshMallowPermission(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

/*AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);*/

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (checkSelfPermission(Manifest.permission.CAMERA)
            != PackageManager.PERMISSION_GRANTED) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.CAMERA},
                    MY_REQUEST_CODE);
        }
    }
}

mScannerView = new ZXingScannerView(this);   // Programmatically initialize the scanner view
setContentView(mScannerView);

mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera();         // Start camera

我尝试使用此代码将横幅放在活动相机的底部,但它不起作用。

RelativeLayout mainLayout = (RelativeLayout)findViewById(R.id.main_layout);
RelativeLayout.LayoutParams rlParams = new RelativeLayout.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT);
rlParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);
AdView mAdView = (AdView) findViewById(R.id.adView);
//AdRequest adRequest = new AdRequest.Builder().build();
//mAdView.loadAd(adRequest)
mainLayout.addView(mAdView,rlParams);
// Load the ad:
//AdRequest adRequest = new AdRequest();
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);

扫描仪视图初始化,但它没有显示横幅,我如何调用相机活动并将横幅放在那里?你的帮助将不胜感激。感谢。

public void QrScanner(View view) {


mScannerView = new ZXingScannerView(this);   // Programmatically initialize the scanner view
setContentView(mScannerView);

mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera();         // Start camera

1 个答案:

答案 0 :(得分:0)

您可以使用setContentView(mScannerView);来充气,而不是使用FrameLayout,您已经在setContentView(R.layout.activity_main);

添加activity_main.xml,添加FrameLayout,添加一个LinearLayout容器来托管扫描程序,并将另一个LinearLayout作为AdMob横幅的容器。

通过调用LinearLayoutmLinearLayoutContainerForScanner.addView(mScannerView);在相应的mLinearLayoutContainerForAdMobBanner.addView(mScannerView)运行时添加您的观看次数。