简体   繁体   English

点击按钮的上半部分 - 不起作用?

[英]Click on the top half of a button - doesn't work?

I have 4 Relative layouts : ( as you can see in the animation ) 我有4个相对布局:( 你可以在动画中看到

  • The green RelativeView 绿色的RelativeView
  • The " type something & icons " RelativeView 输入内容和图标 ”RelativeView
  • The gray speperator RelativeView 灰色拼音器RelativeView
  • The bottom Textview 底部的Textview

Each RelativeView is "below" it's previous relative view. 每个RelativeView都在它之前的相对视图“下方”。

在此输入图像描述

By design , when the 2 inner views are closed , the button should be half top above the green , and half bottom above the text ( just like the animation shows) 按照设计,当两个内部视图关闭时,按钮应该在绿色上方顶,在文本上方半个底部(就像动画节目一样)

Ok , So I added a button which is found inside the "bottom textview" 好的,所以我添加了一个在“底部textview”中找到的按钮

But in order for the bottom to be only half bottom above the view , I added it a negative Margin : 但是为了使底部只在视图上方的一半,我添加了一个负边距:

So here it is without the negative margin : 所以这里 没有负余量:

在此输入图像描述

And here it is with the negative margin ( the desired result) 这里是负余量(期望的结果)

在此输入图像描述

So when I clicked the button I simply hide/show ( + animation with android:animateLayoutChanges="true" ) the inner 2 middle views 所以,当我点击按钮时,我只是隐藏/显示(+动画与android:animateLayoutChanges="true" )内部2中间视图

So where is the problem ? 那问题出在哪里?

Question

I don't know why but only the bottom half of the button is clickable ! 我不知道为什么,但只有按钮的下半部分是可点击的! I guess it is because that half is inside its container view while the top half is not in its view...( maybe i'm wrong) 我想这是因为那一半在容器视图中,而上半部分不在它的视图中...(也许我错了)

But if I remove the negative margin and the button is fully in its container - then the button is 100% fully clickeable ( both top half and bottom half) 但如果我删除了负边距并且按钮完全在其容器中 - 那么按钮是100%完全可以设置的(上半部分和下半部分)

As you can see in the animation (last frames) - when i click the top half - nothing happens.... 正如你在动画中看到的那样(最后一帧) - 当我点击上半部分时 - 没有任何反应......

How can I fix that ? 我该如何解决这个问题?

Maybe i've taken a wrong initial approach ? 也许我采取了错误的初始方法

nb : some more visualization of structure : nb: 更多结构可视化

在此输入图像描述

Have your button as a sibling to the RelativeLayouts rather than as a child. 让你的按钮成为RelativeLayouts的兄弟,而不是孩子。 This works as you want it to. 这可以按照您的意愿运行。

<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>

Looks like this and the button moves up/down as setVisibility is toggled between GONE/VISIBLE for green RelativeLayout 看起来像这样,按钮向上/向下移动,因为setVisibility在GONE / VISIBLE之间切换为绿色RelativeLayout

看起来像这样,按钮向上/向下移动,因为setVisibility在GONE / VISIBLE之间切换为绿色RelativeLayout

Your button belongs to bottom RL. 你的按钮属于底部RL。 When android routes ACTION_DOWN it checks layout's borders and gives events to viewgrops (VG) which have event coordinates inside. 当android路由ACTION_DOWN时,它会检查布局的边框,并向其中包含事件坐标的viewgrops(VG)提供事件。 Then VG proporate event to it's children based on it's coordinates. 然后VG根据它的坐标向它的孩子们提供活动。

So when you click on upper part of your button touch event given to grey RL and button which belonges to blue RL doesn't get it. 所以当你点击按钮的上半部分触摸事件给予灰色RL和按钮蓝色RL没有得到它。 Actually event given to Window -> Root ViewGroup -> Some Other ViewGroup -> View. 实际上事件给了Window - > Root ViewGroup - > Some Other ViewGroup - > View。 And routing happens base on coordinates. 并且路由基于坐标发生。 It is true for ACTION_DOWN which starts touch, but not all MotionEvent processed this way. 对于开始触摸的ACTION_DOWN确实如此,但并非所有以这种方式处理的MotionEvent。

As a solution, you can move button to another groupview which can route touch event properly. 作为解决方案,您可以将按钮移动到另一个可以正确路由触摸事件的组视图。 Or maybe try to use touch delegates. 或者也许尝试使用触摸代表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM