近日在搞一个直播项目,用SurfaceView显示直播远端画面,但是有一个需求就是加一个按钮,可以显示隐藏SurfaceView。
<FrameLayout
android:id="@+id/box"
android:layout_width="160dp"
android:layout_height="90dp"
android:layout_centerInParent="true">
</FrameLayout>
布局中定义FrameLayout,命名为box,布局很简单,我通过直播SDK拿到surfaceview以后,直接
box.addView(surfaceV, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
此时surfaceview显示到画面中了,那如何隐藏呢?
按照常理出牌肯定童鞋会说box.setVisibility(INVISIBLE)就行了,但是此时设置之后你会发现surfaceview没有隐藏,感觉surfaceview脱标了,像单独浮在屏幕上而不依赖父布局。
解决方法:
显示:
box.getChildAt(0).setVisibility(VISIBLE);
box.setVisibility(VISIBLE);
隐藏:
box.getChildAt(0).setVisibility(INVISIBLE);
box.setVisibility(INVISIBLE);
总结:surfaceview比较特殊,需要拿到surfaceview对象设置隐藏和显示,而不能像普通控件只要隐藏父布局就能全部隐藏。