以编程方式设置 SurfaceView 的边距



我在布局xml文件中添加相机的"SurfaceView"作为

<SurfaceView
        android:id="@+id/preview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="95dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"/>

但我想动态设置marginTop。如果我想以编程方式设置SurfaceView的marginTop,我该如何做到这一点?

我在网上搜索过。但我没有得到任何答案。所以任何人帮我做这件事。

您可以执行以下操作:

int margin = getResources().getDimensionPixelSize(R.dimen.surface_view_margin);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
lp.setMargins(margin, 0, 0, 0);
surfaceView.setLayoutParams(lp);

这有帮助吗?

最新更新