Android SurfaceView上方的控件不显示

最近在做一个项目,在SurfaceView展示视频内容的时候,右上角有一个摄像机的图标,理想的效果图如下:

理想效果

代码如下:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="228dp">
    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:cardCornerRadius="18dp">
        <SurfaceView
                android:id="@+id/sv_video"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
    </androidx.cardview.widget.CardView>
    <ImageView
            android:layout_width="24dp"
            android:layout_height="18.72dp"
            android:layout_gravity="end"
            android:layout_marginTop="13dp"
            android:layout_marginEnd="18dp"
            android:src="@mipmap/ic_main_video"/>
</FrameLayout>

然而实际上运行起来是这样的(右上角的ImageView就如同不存在一样):

实际效果

网上有很多解决方案,如代码设置setZOrderOnTopsetZOrderMediaOverlay等,但在这个项目里却不生效,后来发现一个简单的解决方案,就是用CardViewImageView包裹起来即可,代码如下:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="228dp">
    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:cardCornerRadius="18dp">
        <SurfaceView
            android:id="@+id/sv_video"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </androidx.cardview.widget.CardView>
    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:backgroundTint="@android:color/transparent"
        android:background="@android:color/transparent">
        <ImageView
            android:layout_width="24dp"
            android:layout_height="18.72dp"
            android:layout_gravity="end"
            android:layout_marginTop="13dp"
            android:layout_marginEnd="18dp"
            android:src="@mipmap/ic_main_video"/>
    </androidx.cardview.widget.CardView>
</FrameLayout>

这是一个风趣幽默的博主,不定时分享技术和生活干货,赶紧关注一波吧~~

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容