Android ScrollView添加SurfaceView背景问题

在Android开发中,将SurfaceView放入ScrollView时遇到滚动时背景变为黑色的问题。设置SurfaceView背景颜色会导致绘制内容被遮挡。解决方法:

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当在ScrollView里添加SurfaceView时,拖动背景会变黑色:

如果SurfaceView设置其他颜色背景会遮挡绘图的内容。

解决:

1.在代码中设置:SurfaceView.setZOrderOnTop(true);
2.获取SurfaceHolder对象再设置背景透明:
      SurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
    也可以在xml里设置背景透明:
     <SurfaceView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#00000000" />


<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/gradient_background"> <!-- 标题栏 --> <RelativeLayout android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?android:attr/actionBarSize" android:background="#44bd32"> <Button android:id="@+id/fanHui" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" android:background="@android:color/transparent" android:foreground="?android:attr/selectableItemBackground" android:text="返回" android:textColor="@android:color/white" android:textSize="16sp" /> </RelativeLayout> <!-- 底部按钮 --> <LinearLayout android:id="@+id/footer_buttons" android:layout_width="match_parent" android:layout_height="1dp" android:layout_alignParentBottom="true" android:background="@android:color/white" android:orientation="horizontal"> <!-- <Button--> <!-- android:id="@+id/device_location"--> <!-- android:layout_width="match_parent"--> <!-- android:layout_height="match_parent"--> <!-- android:gravity="center"--> <!-- android:text="设备定位"--> <!-- android:textSize="19sp"--> <!-- android:background="#44bd32"--> <!-- android:textColor="@android:color/white" />--> </LinearLayout> <!-- 图片网格容器(添加滚动支持) --> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/footer_buttons" android:layout_below="@id/toolbar" android:background="@android:color/white" android:fillViewport="true" android:padding="0dp"> <GridLayout android:id="@+id/image_grid" android:layout_width="match_parent" android:layout_height="wrap_content" android:columnCount="2" android:padding="0dp" /> </ScrollView> </RelativeLayout> 修改图片网格容器<ScrollView android:layout_width="match_parent" android:layout_height="380dp" android:layout_above="@id/footer_buttons" android:layout_below="@id/toolbar" android:layout_marginBottom="298dp" android:background="@android:color/white" android:fillViewport="true" android:padding="0dp"> </ScrollView>成一个播放视频的模块,其它不变。
最新发布
06-27
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值