首页 > 解决方案 > 如果显示所有组件并且 Android API 27 中还有空间,则 onClick 事件在 LinearLayout 中不起作用

问题描述

我正在使用可用于模拟设备的最大屏幕尺寸的 Android API 27 测试我的应用程序。

如果发生某种情况,如果用户在空白空间(没有 UI 元素的空间)或被动元素(如 TextView)中按下,我的应用程序应该能够做出反应。

从 API 18 到 26,如果在 LinearLayout 中的最后一个元素显示之后还有空间,如果用户按下它,它会做出相应的反应,但在 API 27 中它只是忽略它,但如果点击 TextView 之类的东西就可以工作。

我试图通过创建一个可以填充所有可用空间的不可见 TextView 来解决这个问题,要么将其放在最后一个元素上,要么将其放在中间,但它不起作用,我已经完成了这两种方式。

  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:focusable="true"
        android:focusableInTouchMode="false"
        android:clickable="true">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/linearlayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity4"
        android:orientation="vertical"
        android:focusable="true"
        android:focusableInTouchMode="false"
        android:clickable="true">
        [moreelements]
         <TextView
        android:id="@+id/textViewextra"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="28dp"
        android:gravity="center"
        android:text=""
        android:layout_weight="1"/>
    </LinearLayout>

    </ScrollView>

另一方面:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:focusable="true"
        android:focusableInTouchMode="false"
        android:clickable="true">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/linearlayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity4"
        android:orientation="vertical"
        android:focusable="true"
        android:focusableInTouchMode="false"
        android:clickable="true">
        [moreelements]
         <TextView
        android:id="@+id/textViewextra"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="28dp"
        android:gravity="center"
        android:text=""
        android:layout_weight="1"/>
        [moreelements]
    </LinearLayout>



 </ScrollView>

但在 API 27 中,它甚至不会填满屏幕,并且仍然没有反应。

这个问题怎么解决?

标签: androidandroid-layouteventsandroid-linearlayout

解决方案


您本质上是希望空白空间可点击并执行一些操作?

<View
        android:id="@+id/someId"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

将填充空白空间,然后为其设置一个 OnClickListener。


推荐阅读