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

Android持久化BottomSheet上滑展开时如何实现深色透明背景?

解决方案

我刚好做过类似的需求,给你一套完整的实现方案,既能在BottomSheet展开时给背景加半透明遮罩,还能随滑动过程实现平滑的变暗动画,完全符合你的要求。

第一步:修改布局,添加背景遮罩View

首先在你的主布局里,给主内容和BottomSheet之间加一个全屏的遮罩View,初始状态是完全透明的。注意层级:遮罩要放在主内容的上层、BottomSheet的下层,这样遮罩只会覆盖主内容,不会影响BottomSheet的交互。

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- 你的原有主内容布局 -->
    <LinearLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        <!-- 你的主内容属性 -->
        >
        <!-- 主内容控件 -->
    </LinearLayout>

    <!-- 新增的背景遮罩 -->
    <View
        android:id="@+id/background_dimmer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#80000000" <!-- 半透明黑色,你可以调整透明度值 -->
        android:alpha="0" <!-- 初始完全透明 -->
        android:clickable="true" <!-- 可选:点击遮罩让BottomSheet折叠 -->
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <!-- 你的原有BottomSheet布局 -->
    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:behavior_hideable="false"
        app:behavior_peekHeight="85dp"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
        <!-- BottomSheet内容 -->
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

第二步:监听BottomSheet状态,实现平滑动画

接下来在Activity/Fragment里,通过BottomSheetBehavior的回调监听滑动和状态变化,动态调整遮罩的透明度:

Kotlin代码示例

// 获取BottomSheet和遮罩View
val bottomSheet = findViewById<ConstraintLayout>(R.id.bottom_sheet)
val dimmerView = findViewById<View>(R.id.background_dimmer)
val bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet)

// 添加BottomSheet回调
bottomSheetBehavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
    override fun onStateChanged(bottomSheet: View, newState: Int) {
        // 当BottomSheet折叠时,隐藏遮罩
        if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
            dimmerView.animate().alpha(0f).setDuration(200).start()
        }
        // 当BottomSheet完全展开时,确保遮罩完全显示
        else if (newState == BottomSheetBehavior.STATE_EXPANDED) {
            dimmerView.animate().alpha(1f).setDuration(200).start()
        }
    }

    override fun onSlide(bottomSheet: View, slideOffset: Float) {
        // slideOffset值范围:折叠时为0,完全展开时为1,刚好对应透明度的变化
        // 这样就能实现随滑动过程平滑变暗/变亮的效果
        dimmerView.alpha = slideOffset
    }
})

// 可选:点击遮罩让BottomSheet折叠
dimmerView.setOnClickListener {
    bottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
}

Java代码示例

// 获取BottomSheet和遮罩View
ConstraintLayout bottomSheet = findViewById(R.id.bottom_sheet);
View dimmerView = findViewById(R.id.background_dimmer);
BottomSheetBehavior<ConstraintLayout> bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);

// 添加BottomSheet回调
bottomSheetBehavior.addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
    @Override
    public void onStateChanged(@NonNull View bottomSheet, int newState) {
        if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
            dimmerView.animate().alpha(0f).setDuration(200).start();
        } else if (newState == BottomSheetBehavior.STATE_EXPANDED) {
            dimmerView.animate().alpha(1f).setDuration(200).start();
        }
    }

    @Override
    public void onSlide(@NonNull View bottomSheet, float slideOffset) {
        dimmerView.setAlpha(slideOffset);
    }
});

// 可选:点击遮罩折叠BottomSheet
dimmerView.setOnClickListener(v -> bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED));

关键细节说明

  • slideOffset参数是BottomSheet从折叠状态(peekHeight)到完全展开状态的比例,刚好可以直接用来控制遮罩的透明度,实现完美的平滑过渡。
  • 遮罩的背景色#80000000是半透明黑色,你可以根据需求调整透明度(比如#60000000更浅,#AA000000更深)。
  • 如果不需要点击遮罩折叠BottomSheet,去掉android:clickable="true"和点击事件即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 15:32:36