大家好,又见面了,我是你们的朋友全栈君。
布局代码:
<?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">
<SurfaceView
android:id="@+id/sv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
java代码动态改变SurfaceView大小:
ViewGroup.LayoutParams layoutParams = mSurfaceView.getLayoutParams();
layoutParams.width = width;
mSurfaceView.setLayoutParams(layoutParams);
发现并没有用,但是在SurfaceView外层再嵌套一层FrameLayout就可以了,调整后布局如下:
<?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:keepScreenOn="true">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<SurfaceView
android:id="@+id/sv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</RelativeLayout>
原因不详,知道的小伙伴麻烦留言一下。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/160836.html原文链接:https://javaforall.cn