android - LinearLayout onClick 有效,但状态没有改变

标签 android android-layout onclick

布局具有以下结构:

 <LinearLayout
        android:id="@+id/card_pack_clickable_area"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@drawable/card_pack_bg_selector">

            <FrameLayout
                android:id="@+id/card_pack_body_frame_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/bg_card_pack_body">

                <...>

            </FrameLayout>

            <TextView
            android:id="@+id/card_pack_percentage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="@color/bg_card_pack_body"/>

  </LinearLayout>

@drawable/card_pack_bg_selector"的内容:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:state_pressed="true">
      <shape>
          <solid android:color="#536dfe" />
      </shape>
  </item>

  <item android:state_focused="true">
      <shape>
          <solid android:color="#536dfe" />
      </shape>
  </item>

  <item android:drawable="@android:color/transparent"/>

</selector>

问题陈述

所以 LinearLayout card_pack_clickable_area 有两个元素:FrameLayout card_pack_body_frame_layout 和 TextView card_pack_percentage

我想要实现的是将 FrameLayout 和 TextView 作为单个可点击区域,这就是我将它们包装到外部 LinearLayout 中的原因。

在 Java 代码中,我将 onClickListener 放在这个 LinearLayout 上。实际上,当我单击 FrameLayout 或 TextView 时,我正在获取 onClick 事件。

问题是 LinearLayout 没有改变它的状态,因为它应该跟随它的背景(@drawable/card_pack_bg_selector")。

问题

如何将 FrameLayout 和 TextView 作为具有单一背景的联合可点击区域,从而改变其按下/未按下状态?

最佳答案

能否请您尝试将 android:clickable="true" 添加到您的外部线性布局中。

如果不起作用,您还可以尝试再次将 android:descendantFocusability="beforeDescendants" 添加到您的外部布局中。

祝你好运

关于android - LinearLayout onClick 有效,但状态没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29642984/

相关文章:

android - 如何在项目 gradle.properties 中设置 org.gradle.jvmargs=-Xmx5120M?

android - 如何为 10"以下的选项卡自定义应用程序首选项屏幕(双 Pane 布局)

android - 小部件在主屏幕中拉伸(stretch)。

android - 使用 RelativeLayout 垂直平均分割两个 fragment

javascript - 在 React 中,如何使用自己的参数调用多个函数?

php - GCM 设置推送通知的到期日期

安卓降级问题

javascript - 设置一个数组来选择不同的变量onclick

android - 在查看/解析 IIS 日志时,如何过滤掉来自移动应用程序而不是 Web 浏览器的对 API 的直接调用

JavaScript onClick 更改 css