简体   繁体   English

当Android上的按钮处于LinearLayout时,onClick不会触发

[英]onClick not triggering when button is in LinearLayout on Android

So I'm trying to make something similar to how snapchat does the options menu where you slide down and you can still see the camera in the background. 所以我试图做一些类似于Snapchat滑动的选项菜单,你仍然可以在后台看到相机。 I won't post all the code, but I've debugged what it is that's causing a problem. 我不会发布所有代码,但我已经调试了导致问题的原因。 I'm a noob to android dev, but I've done some java programming before. 我是android开发者的菜鸟,但之前我做过一些java编程。

This is my xml 这是我的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>

I add the onClick listener within my MainMenu.java class which extends RelativeLayout. 我在MainMenu.java类中添加了onClick侦听器,该类扩展了RelativeLayout。 Here is a gist with the rest of the code https://gist.github.com/colmex/1bd19dd2cfc400b2f870 以下是其余代码的要点https://gist.github.com/colmex/1bd19dd2cfc400b2f870

The issue is that when the @id/take_image_nav_button is in the LinearLayout, the onClick action doesn't work, when I take it out of it's LinearLayout, the onClick action does work. 问题是,当@ id / take_image_nav_button在LinearLayout中时,onClick操作不起作用,当我从它的LinearLayout中取出时,onClick操作确实有效。 It feels like the LinearLayout is somehow blocking the click. 感觉就像LinearLayout以某种方式阻止了点击。 I've tried a lot of things I've found on stackoverflow and other googling, but nothing seems to fix it. 我已经尝试了很多我在stackoverflow和其他谷歌搜索上发现的东西,但似乎没有什么能解决它。

On the LinearLayout parent to the button you have set android:clickable="false" , but this makes everything inside it unclickable too. 在按钮的LinearLayout父级上,你设置了android:clickable="false" ,但这使得其中的所有内容都无法点击。 To fix it, change false to true 要修复它,请将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

Or simply remove this attribute. 或者只是删除此属性。

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

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