You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ZXing Scanner View添加按钮不显示问题求助

解决ZXing扫描视图上按钮不显示的问题

Hey there! Let's break down why your flash buttons aren't showing up and fix it right away.

问题根源

The ZXingScannerView from the dm7 barcode scanner library is a subclass of SurfaceView. SurfaceViews have a unique behavior: they render on a separate layer, and crucially, they don't support hosting child views directly. When you nest your ImageButtons inside the ZXingScannerView, they never get rendered—because the SurfaceView's own camera feed completely covers anything you put inside it.

修复方案

You need to restructure your layout so the scanner view and buttons are siblings in the same parent layout. This way, the buttons will draw on top of the scanner view. Here's the corrected XML:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#CFD8DC"
    tools:context="com.example.adisi.turzilnic.S1">

    <!-- Scanner view sits as the base layer -->
    <me.dm7.barcodescanner.zxing.ZXingScannerView
        android:id="@+id/zxscan"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <!-- Flash off button positioned where you want -->
    <ImageButton
        android:id="@+id/FlashOFF"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/flashoff"
        android:layout_marginStart="150dp"
        android:layout_marginTop="150dp"/>

    <!-- Flash on button centered horizontally -->
    <ImageButton
        android:id="@+id/FlashON"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/flashon"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"/>

</RelativeLayout>

额外提示

  • Once you update the layout, your buttons will appear on top of the scanner view. You can adjust their position using standard RelativeLayout attributes (like layout_alignParentBottom or layout_margin) to get them exactly where you need.
  • Don't forget to add click listeners to the buttons to control the flash! You can use mScannerView.setFlash(true) to turn it on and mScannerView.setFlash(false) to turn it off.

内容的提问来源于stack exchange,提问作者Adrian Geanta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:18:56