ZXing Scanner View添加按钮不显示问题求助
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_alignParentBottomorlayout_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 andmScannerView.setFlash(false)to turn it off.
内容的提问来源于stack exchange,提问作者Adrian Geanta

