【问题标题】:onClick not triggering when button is in LinearLayout on Android当按钮在Android上的LinearLayout中时,onClick不会触发
【发布时间】:2015-11-21 04:07:25
【问题描述】:

所以我正在尝试制作类似于 snapchat 如何在您向下滑动的选项菜单中进行的操作,您仍然可以在后台看到相机。我不会发布所有代码,但我已经调试了导致问题的原因。我是android dev的菜鸟,但我以前做过一些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 动作不起作用,当我将它从LinearLayout 中取出时,onClick 动作起作用。感觉就像 LinearLayout 以某种方式阻止了点击。我已经尝试了很多我在 stackoverflow 和其他谷歌搜索上发现的东西,但似乎没有任何东西可以解决它。

【问题讨论】:

标签: java android android-layout android-button


【解决方案1】:

在您设置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

或者干脆删除这个属性。

【讨论】:

  • 刚试了没用,也试过拿出来也没用。更新帖子
  • @LuisFHernandez 好吧,对不起。如果你把它放在这个线性布局的正上方,它就会变成可点击的,对吧?因为在第一个布局上还有另一个android:clickable="false",但如果按钮在其中工作之前我可能会错误地删除此属性。
  • 我取出了另一个android:clickable="false",我没有注意到它在那里。基本上,如果我将@id/take_image_nav_button 放在它的父LinearLayout 之外并将它放在@id/slide_menuRelativeLayout 中,它就可以工作,我可以点击它。但是当它在 @id/slide_menuRelativeLayout 内的 LinearLayout 内时,它不起作用,我无法单击它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-02
  • 1970-01-01
  • 2013-06-09
  • 2014-06-15
相关资源
最近更新 更多