在 kotlin 的 canvas 视图中添加一个按钮

add a button in a canvas view in kotlin

这是main_activity文件的内容

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val myCanvasView = MyCanvasView(this)
        setContentView(R.layout.activity_main)
        //R.layout.activity_main
//        val button: Button = findViewById<Button>(R.id.button)
    }
}

我创建了一个自定义的canvas视图,这里是代码

class MyCanvasView(context: Context) : View(context) {
    private var path = Path()
    // variable for holding the color to draw with
    private val drawColor = ResourcesCompat.getColor(resources, R.color.colorPaint, null)

    // To store the x and y coordinate where the user touched the screen
    private var motionTouchEventX = 0f
    private var motionTouchEventY = 0f

    // the x and y coordinate where the user lifts their finger from the screen
    private var currentX = 0f
    private var currentY = 0f

    private val touchTolerance = ViewConfiguration.get(context).scaledTouchSlop
    private lateinit var extraCanvas: Canvas
    private lateinit var extraBitmap: Bitmap

现在我想向该视图添加一个按钮。我在 activity_mail.xml 文件

中做了哪些更改
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="88dp"
        android:layout_height="48dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="160dp"
        android:text="@string/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/canvas"
        class="MyCanvasView"
        android:layout_width="415dp"
        android:layout_height="488dp"
        android:layout_below="@+id/button"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button"
        tools:ignore="ExtraText,MissingClass,MissingConstraints"
        tools:layout_editor_absoluteX="-2dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

我应该对代码进行哪些更改才能显示按钮以及分配给 canvas 视图的屏幕的特定部分?

ConstraintLayout 可以使一个视图对后面定义的视图进行约束。这是一个从上到下制作按钮和 your_view 的示例。

<Button id="@+id/button"
  app:layout_constraintLeft_toLeftOf="parent"
  app:layout_constraintTop_toTopOf="parent"
  app:layout_constraintBottom_toTopOf="@id/your_view"
  />

<YourView id="@+id/your_view"
  app:layout_constraintTop_toBottomOf="@id/button"
  app:layout_constraintBottom_toBottomOf="parent"
  app:layout_constraintLeft_toLeftOf="parent"
  />