【发布时间】:2025-12-09 02:05:01
【问题描述】:
我正在尝试实现一个自定义视图,用户可以在其中用手指绘图。基本上,我在关注this tutorial。
一切似乎都很好,但是,考虑到我在自定义视图下方也有 2 个按钮,如下所示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_color"
tools:context=".MainActivity"
android:id="@+id/mainScreen" >
<Button
android:id="@+id/buttonSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:text="@string/save" />
<Button
android:id="@+id/buttonQuery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="@string/query" />
<com.example.myapp.DrawView
android:id="@+id/drawView"
android:background="@color/red"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_above="@id/buttonSave" />
</RelativeLayout>
它实际上甚至在按钮下方绘制,我很难理解为什么它不只捕获发生在 DrawView 而不是在它之外的触摸事件。基本上,我不能从按钮上方开始画线,但是一旦我开始在我的视图上绘制,我什至可以将线延伸到它之外。
据我所知,DrawView 不会在按钮下方溢出,as discussed here 所以我想弄清楚发生了什么。我真的必须检查事件是否超出范围吗?它实际上是否将所有触摸事件委托给当前聚焦的视图?有没有办法改变这种行为?
This simple tutorial 没有描述的问题,但它会逐点绘制。
更新: 调试应用程序时,我确定 drawView 的大小为 480x728,而 mainScreen 的大小为 480x800,因此它们不重叠。一旦获得焦点,即使它们在drawView之外触发,它也会继续注册触摸事件。这是我要避免的,最好不要通过大的 if 语句传递事件坐标...
【问题讨论】:
标签: android android-layout user-interface touch