我有一个RelativeLayout,并希望禁用任何触摸事件的较低层,然而,将android:clickable="true"放在较高的级别并不能做到这一点。请您看一下:
<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" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="lower layer button" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="upper layer button"/>
</RelativeLayout>
</RelativeLayout>我仍然可以点击这两个按钮。这段代码可能有什么问题?
编辑以使问题更清楚。。
发布于 2015-04-22 18:38:19
过了一段时间我找到了答案:
我的第二层没有拦截所有的触摸事件,因为它比第一层小。因此,尽管android:clickable=“真”是必要的,但这还不够。底层不应大于上层。使第二个RelativeLayout大小可继承修复了以下问题:
<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" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="lower layer button" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="upper layer button"/>
</RelativeLayout>
</RelativeLayout>发布于 2015-04-21 22:05:07
默认情况下,按钮是可点击的。如果希望按钮不可单击,则必须显式地将属性设置为false:
<!-- this will be clickable -->
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test test test"
android:clickable="true" />
<!-- this will not be clickable -->
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false" />https://stackoverflow.com/questions/29783923
复制相似问题