如何在 Android 中禁用布局的所有触摸

How to disable all touches for a Layout in Android

我正在尝试在 Xamarin.Android 中实现底部 sheet 并且我想禁用所有非底部 sheet(即主布局)的所有触摸,当底部 sheet 处于活动状态。

Main.axml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/main_layout">
<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />

<!-- Main Layou -->
<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/main_content"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" >
    <Button
        android:id="@+id/btn_test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/logout"
        android:layout_gravity="center" />
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="16dp"
        android:clickable="true"
        android:src="@drawable/ic_plus" />
</FrameLayout>

<FrameLayout
    android:id="@+id/tint"
    android:background="#000000"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<!-- Bottom sheet -->
<FrameLayout
    android:id="@+id/bottom_fragment_container"
    android:layout_width="match_parent"
    android:layout_height="160dp"
    android:background="#E0E0E0"
    android:layout_gravity="bottom"
    android:elevation="16dp"
    android:translationY="160dp" />

我尝试遍历所有子项并禁用它们,但这会改变按钮的外观,我想避免这种情况。我也尝试将布局的 Enabled 属性 设置为 false,但按钮仍然可以触摸。

我想我需要以某种方式在所有触摸到达按钮之前拦截它们(以及稍后将出现的所有其他触摸),但我不知道该怎么做。通过拦截所有触摸,我还可以在这些触摸后隐藏底部 sheet。

创建一个覆盖整个屏幕的透明 RelativeLayout 并在其中使用 android:clickable="true" 属性 以确保它拦截所有点击事件。

然后将android:layout_alignParentBottom添加到你的底部sheet,并将其放入这个新创建的RelativeLayout中。这样它就会出现在底部,其他所有内容都无法点击。

<FrameLayout>

  <!-- Main Layout -->


  <RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0000"
    android:clickable="true">

     <FrameLayout
       android:id="@+id/bottom_fragment_container"
       android:layout_width="match_parent"
       android:layout_height="160dp"
       android:background="#E0E0E0"
       android:layout_alignParentBottom="true"
       android:elevation="16dp"
       android:translationY="160dp" />

  </RelativeLayout>

</FrameLayout>