[英]Click on the top half of a button - doesn't work?
我有4个相对布局:( 你可以在动画中看到 )
每个RelativeView都在它之前的相对视图“下方”。
按照设计,当两个内部视图关闭时,按钮应该在绿色上方半顶,在文本上方半个底部(就像动画节目一样)
好的,所以我添加了一个在“底部textview”中找到的按钮
但是为了使底部只在视图上方的一半,我添加了一个负边距:
所以这里 没有负余量:
这里是负余量(期望的结果)
所以,当我点击按钮时,我只是隐藏/显示(+动画与android:animateLayoutChanges="true"
)内部2中间视图
那问题出在哪里?
题
我不知道为什么,但只有按钮的下半部分是可点击的! 我想这是因为那一半在容器视图中,而上半部分不在它的视图中...(也许我错了)
但如果我删除了负边距并且按钮完全在其容器中 - 那么按钮是100%完全可以设置的(上半部分和下半部分)
正如你在动画中看到的那样(最后一帧) - 当我点击上半部分时 - 没有任何反应......
我该如何解决这个问题?
也许我采取了错误的初始方法 ?
nb: 更多结构可视化 :
让你的按钮成为RelativeLayouts的兄弟,而不是孩子。 这可以按照您的意愿运行。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true">![enter image description here][1]
<RelativeLayout
android:id="@+id/red"
android:background="@android:color/holo_red_dark"
android:layout_width="match_parent"
android:layout_height="100dp">
</RelativeLayout>
<RelativeLayout
android:id="@+id/green"
android:layout_below="@id/red"
android:background="@android:color/holo_green_dark"
android:layout_width="match_parent"
android:layout_height="100dp">
</RelativeLayout>
<RelativeLayout
android:id="@+id/blue"
android:background="@android:color/holo_blue_dark"
android:layout_below="@id/green"
android:layout_width="match_parent"
android:layout_height="100dp">
</RelativeLayout>
<Button
android:id="@+id/button"
android:layout_centerHorizontal="true"
android:layout_below="@id/green"
android:layout_width="wrap_content"
android:text="Hide Green"
android:layout_marginTop="-24dp"
android:layout_height="wrap_content"/>
</RelativeLayout>
看起来像这样,按钮向上/向下移动,因为setVisibility在GONE / VISIBLE之间切换为绿色RelativeLayout
你的按钮属于底部RL。 当android路由ACTION_DOWN时,它会检查布局的边框,并向其中包含事件坐标的viewgrops(VG)提供事件。 然后VG根据它的坐标向它的孩子们提供活动。
所以当你点击按钮的上半部分触摸事件给予灰色RL和按钮蓝色RL没有得到它。 实际上事件给了Window - > Root ViewGroup - > Some Other ViewGroup - > View。 并且路由基于坐标发生。 对于开始触摸的ACTION_DOWN确实如此,但并非所有以这种方式处理的MotionEvent。
作为解决方案,您可以将按钮移动到另一个可以正确路由触摸事件的组视图。 或者也许尝试使用触摸代表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.