java - 当按钮在 Android 上的 LinearLayout 中时,onClick 不会触发

标签 java android android-layout android-button

所以我正在尝试做一些类似于 snapchat 的选项菜单,你向下滑动时你仍然可以在背景中看到相机。我不会发布所有代码,但我已经调试了导致问题的原因。我是 android 开发的菜鸟,但我以前做过一些 java 编程。

这是我的xml

<me.dontenvy.videotest.MainMenu xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000"
    android:orientation="vertical"
    android:animateLayoutChanges="true"
    android:id="@+id/main_menu">

<!-- this is for the camera -->
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/camera_container">

    <TextureView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/texture"
        android:animateLayoutChanges="true"/>

</RelativeLayout>

<!-- this is for the overlay menu -->
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/slide_menu"
    android:animateLayoutChanges="true"
    android:background="@drawable/transparent_background">

    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/clear_background"
        android:layout_alignParentBottom="true"
        android:id="@+id/slide_button"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <Button
            android:layout_width="@dimen/circle_nav_button_diameter"
            android:layout_height="@dimen/circle_nav_button_diameter"
            android:background="@drawable/circle_button"
            android:backgroundTint="#050"
            android:id="@+id/take_image_nav_button"/>
    </LinearLayout>
</RelativeLayout>
</me.dontenvy.videotest.MainMenu>

我在扩展 RelativeLayout 的 MainMenu.java 类中添加了 onClick 监听器。这是其余代码的要点 https://gist.github.com/colmex/1bd19dd2cfc400b2f870

问题是当@id/take_image_nav_button 在 LinearLayout 中时,onClick Action 不起作用,当我将它从 LinearLayout 中取出时,onClick Action 起作用。感觉 LinearLayout 以某种方式阻止了点击。我已经尝试了很多我在 stackoverflow 和其他谷歌搜索上发现的东西,但似乎没有什么可以解决的。

最佳答案

在您已设置 android:clickable="false" 的按钮的 LinearLayout 父级上,但这也使得其中的所有内容也无法点击。要修复它,请将 false 更改为 true

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:clickable="true"> // Change here to true, like this

或者干脆去掉这个属性。

关于java - 当按钮在 Android 上的 LinearLayout 中时,onClick 不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32238125/

相关文章:

java - Android Studio 按钮导致崩溃

android - 支持没有硬编码比例的最大屏幕纵横比?

android - 如何在android CalendarView中显示json响应?

android - 如何从我的服务类中获取显示对象?

android - 您需要在此 Activity 中使用 Theme.AppCompat 主题(或后代)

java - 类(Class)冲突。加工

java - 如何在新方法中使用两种不同方法中的变量?

java - Maven - 使用最后一个双参数

java - 如何使用Spring data JPA(JPARepository)实现延迟加载?

java - 如何在 Android 中偏移 RecyclerView 的第二列?