xml :
<RelativeLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#" >
<SurfaceView
android:id="@+id/video_surface"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
コード
RelativeLayout.LayoutParams layoutParams=new RelativeLayout.LayoutParams();
videoSurface.setLayoutParams(layoutParams);
コード分析:
ここでは、サイズを変更したいビューのレイアウトを知る必要があります。
ビューのサイズを制御するために、異なる幅と高さのパラメータを渡す/変更することができます。
最適化
LayoutParamsの型が正しくない場合、プログラムは例外をスローし、そのたびにレイアウトにアクセスして対応する親レイアウトを探し、対応するLayoutParamsオブジェクトを生成しなければなりません。
最適化:リフレクションを使用して、ビューに対応するLayoutParamsオブジェクトを生成します。
LayoutParams のコードを反映します:
Class<? extends ViewGroup.LayoutParams> LayoutParamsClass=videoSurface.getLayoutParams().getClass();
videoSurface.setLayoutParams( LayoutParamsClass.getDeclaredConstructor(int.class,int.class).newInstance());