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

本文关键字:布局 触摸 Android | 更新日期: 2023-09-27 18:33:20

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

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 ,但按钮仍然可以触摸。

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

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

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

然后将android:layout_alignParentBottom添加到您的底表并将其放入这个新创建的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>