You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MaterialToolbar下方区域无法拖动展开后的BottomSheet问题求助

问题解决:BottomSheet展开后Toolbar对应区域无法拖动收起

问题原因

你的布局中,AppBarLayout(包含MaterialToolbar)与BottomSheet同属CoordinatorLayout的子View,当BottomSheet展开时,Toolbar区域会覆盖在BottomSheet上方。由于Toolbar默认会拦截触摸事件,导致该区域的拖动事件无法传递给BottomSheetBehavior,因此无法拖动收起BottomSheet。

解决方案

方案1:让Toolbar区域穿透触摸事件(无Toolbar交互需求时使用)

如果你的Toolbar不需要任何点击、菜单交互,可以直接给AppBarLayout和MaterialToolbar添加以下属性,让触摸事件穿透到底层的BottomSheet:

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false">

    <com.google.android.material.appbar.MaterialToolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="128dp"
        app:layout_collapseMode="none"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false" />

</com.google.android.material.appbar.AppBarLayout>

方案2:保留Toolbar交互的同时传递拖动事件

如果需要保留Toolbar的点击、菜单功能,可在代码中给Toolbar添加触摸监听,将拖动事件传递给BottomSheetBehavior:

Kotlin代码示例

import android.view.MotionEvent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.google.android.material.bottomsheet.BottomSheetBehavior
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val bottomSheetBehavior = BottomSheetBehavior.from(standard_bottom_sheet)
        
        toolbar.setOnTouchListener { _, event ->
            // 将触摸事件传递给BottomSheetBehavior处理
            bottomSheetBehavior.onTouchEvent(coordinator_layout, event)
            // 返回false以保留Toolbar自身的点击事件
            false
        }
    }
}

Java代码示例

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.appbar.MaterialToolbar;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View bottomSheet = findViewById(R.id.standard_bottom_sheet);
        BottomSheetBehavior<View> bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
        MaterialToolbar toolbar = findViewById(R.id.toolbar);

        toolbar.setOnTouchListener((v, event) -> {
            bottomSheetBehavior.onTouchEvent((ViewGroup) v.getParent(), event);
            return false;
        });
    }
}

方案3:调整布局的fitsSystemWindows属性

尝试移除AppBarLayout的android:fitsSystemWindows="true"属性,避免系统窗口Insets导致的触摸区域计算异常:

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <!-- 保留Toolbar其他属性 -->
</com.google.android.material.appbar.AppBarLayout>

内容的提问来源于stack exchange,提问作者Steve M

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 08:03:02