【问题标题】:Disable response of root view in android在android中禁用根视图的响应
【发布时间】:2015-04-22 22:47:56
【问题描述】:

我有一个ListView 有几行。每行包含 7 个 TextViews,它们对 onClick() 事件做出反应。

这一切都很好,但是当用户点击行的边缘时,没有TextView 捕获onClick() 事件,根视图 - LinearLayout - 突出显示

这当然是正常行为,但由于单击此处没有任何反应,我不希望突出显示线性布局。 有什么方法可以禁用此行为继续捕捉TextViews 上的onClick() 事件

(onClick 监听器设置在适配器的 getView() 方法中)

这里是 xml 文件的一些摘录。正如人们所看到的,我尝试了一些方法,但它们不起作用。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"

    android:longClickable="false"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false">


    <TextView
        android:id="@+id/listelement_weekoverview_tv_mo"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="1dp"
        android:layout_weight="1"
        android:background="@drawable/weeklylist_rndrectangle"
        android:gravity="center_horizontal"
        android:singleLine="false"
        android:textColor="@color/appcolor_red_base" />

        ...
</LinearLayout>

未点击版本 点击版本

【问题讨论】:

  • 这听起来像个 hack,但您可以将行项目的背景设置为始终具有相同颜色的可绘制资源。
  • @Emmanuel 我尝试将其设置为 (aet)android:color/transparent 但这不起作用,我想保持透明。
  • 您指定的颜色不是完全不透明的。颜色也不是可绘制的。你想使用state list drawable

标签: android xml layout view


【解决方案1】:

我找到了解决办法。

只需像这样覆盖自定义适配器中的isEnabled (int position) 方法:

@Override
public boolean isEnabled (int position) {
    return false;
}

【讨论】:

  • 比我的方法更好!
猜你喜欢
  • 2018-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-17
相关资源
最近更新 更多