首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >android:clickable不起作用

android:clickable不起作用
EN

Stack Overflow用户
提问于 2015-04-21 21:58:58
回答 2查看 297关注 0票数 0

我有一个RelativeLayout,并希望禁用任何触摸事件的较低层,然而,将android:clickable="true"放在较高的级别并不能做到这一点。请您看一下:

代码语言:javascript
复制
<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>

我仍然可以点击这两个按钮。这段代码可能有什么问题?

编辑以使问题更清楚。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-04-22 18:38:19

过了一段时间我找到了答案:

我的第二层没有拦截所有的触摸事件,因为它比第一层小。因此,尽管android:clickable=“真”是必要的,但这还不够。底层不应大于上层。使第二个RelativeLayout大小可继承修复了以下问题:

代码语言:javascript
复制
<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>
票数 0
EN

Stack Overflow用户

发布于 2015-04-21 22:05:07

默认情况下,按钮是可点击的。如果希望按钮不可单击,则必须显式地将属性设置为false:

代码语言:javascript
复制
<!-- 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" />
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29783923

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档