【问题标题】:LinearLayout onClick works, but the state does not changeLinearLayout onClick 有效,但状态不变
【发布时间】:2015-06-20 23:40:26
【问题描述】:

布局具有以下结构:

 <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 代码中,我在此 LinearLayout 上放置了 onClickListener。实际上,当我单击 FrameLayout 或 TextView 时,我正在获取 onClick 事件。

问题在于 LinearLayout 没有按照其背景 (@drawable/card_pack_bg_selector") 更改其状态。

问题

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

【问题讨论】:

标签: android android-layout onclick


【解决方案1】:

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

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

祝你好运

【讨论】:

  • 实际上,您的方法不起作用,但它向我提出了一个想法:我应该从内部元素中删除背景属性(在我的情况下是 FrameLayout 和 TextView)。谢谢!
  • 很高兴它启发了你!
【解决方案2】:

也许,基于this question,您必须使您的LinearLayout 可点击?记得在问之前先谷歌 ;-)

【讨论】:

  • 我当然搜索过。上述方法不起作用。
【解决方案3】:

我应该从内部元素(在我的例子中是 FrameLayout 和 TextView)中删除背景属性 (android:background="@color/bg_card_pack_body")。

【讨论】:

    【解决方案4】:

    用你在@drawable/card_pack_bg_selector 中的旧代码替换这段代码,然后试试,我希望它能正常工作....

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:drawable="#536dfe" android:state_focused="true" android:state_pressed="true"/>
        <item android:drawable="#536dfe" android:state_focused="false" android:state_pressed="true"/>
        <item android:drawable="#536dfe" android:state_focused="true"/>
        <item android:drawable="@android:color/transparent" android:state_focused="false" android:state_pressed="false"/>
    
    </selector>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-07
      • 1970-01-01
      • 2018-01-21
      • 1970-01-01
      • 2014-08-16
      • 1970-01-01
      • 2017-04-23
      • 1970-01-01
      相关资源
      最近更新 更多