Android SurfaceView 动态添加 EditText 控件及位置尺寸控制
要在 SurfaceView 上动态添加 EditText 控件并指定位置和尺寸,可以按照以下步骤进行操作:
- 首先,在你的布局文件中,将 SurfaceView 和一个父级布局(例如 RelativeLayout 或 FrameLayout)放在一起,作为容器来包含 EditText 控件。例如:
<RelativeLayout
android:id='@+id/container'
android:layout_width="match_parent"
android:layout_height="match_parent">
<SurfaceView
android:id='@+id/surface_view'
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
- 在你的 Activity 或 Fragment 中,获取 SurfaceView 的引用,并获取容器的引用。例如:
SurfaceView surfaceView = findViewById(R.id.surface_view);
RelativeLayout container = findViewById(R.id.container);
- 创建一个 EditText 对象,并设置其布局参数。例如:
EditText editText = new EditText(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.leftMargin = 100; // 设置 EditText 的左边距
layoutParams.topMargin = 200; // 设置 EditText 的上边距
editText.setLayoutParams(layoutParams);
- 将 EditText 添加到容器中。例如:
container.addView(editText);
这样,你就可以在 SurfaceView 上动态添加 EditText 控件,并指定其位置和尺寸。根据你的需求,可以根据实际情况调整 EditText 的位置和尺寸,并添加其他的控件。
原文地址: https://www.cveoy.top/t/topic/pv7p 著作权归作者所有。请勿转载和采集!