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

Android BottomSheet高度固定内容滚动问题排查求助

BottomSheet高度固定、内容滚动问题排查与解决

相同代码在旧项目中实现的BottomSheet可自适应内容高度且内部内容不滚动,但在新项目中出现异常:BottomSheet高度被固定,内部内容变为可滚动状态,需要修复为高度自适应且内容不可滚动。


问题根源分析

  1. 冗余滚动容器:AddFriendConfirmationFragment布局中嵌套了ScrollView,该控件会强制在内容超出自身高度时启用滚动,同时干扰BottomSheet的高度计算逻辑
  2. BottomSheet状态配置缺失:未明确设置BottomSheet为完全展开状态,新项目依赖的Material库可能默认采用固定高度的半展开模式
  3. 布局层级冲突:Base布局中FrameLayout的初始可见性为gone,可能导致子Fragment布局高度计算异常

解决步骤

1. 移除不必要的ScrollView

直接删除ScrollView容器,将内部的layout_one作为main_layout的直接子View,消除内容滚动的触发源:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bottomsheet_bg"
    android:padding="5dp">

    <!-- 移除原ScrollView,直接保留内容布局 -->
    <RelativeLayout
        android:id="@+id/layout_one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        
        <!-- 原有TextView、EditText等控件保持不变 -->
        <TextView
            android:id="@+id/title2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="16dp"
            android:layout_marginRight="@dimen/padding_10dp"
            android:text="Add Friend to your List?"
            android:textColor="@color/colorPrimaryText"
            android:textSize="18sp" />
            
        <!-- 其余控件省略 -->
    </RelativeLayout>

    <!-- 原有close按钮、container布局保持不变 -->
    <ImageView
        android:id="@+id/close"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:layout_alignParentRight="true"
        android:layout_margin="10dp"
        android:background="@drawable/ripplebg_viewscircle"
        android:padding="6dp"
        app:srcCompat="@drawable/closesvgnormal" />
</RelativeLayout>

2. 强制设置BottomSheet为展开状态

在onCreateDialog中,明确配置BottomSheetBehavior为完全展开模式,并禁用拖动功能:

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);

    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            BottomSheetDialog d = (BottomSheetDialog) dialog;
            FrameLayout bottomSheet = (FrameLayout) d.findViewById(com.google.android.material.R.id.design_bottom_sheet);
            
            if (bottomSheet != null) {
                BottomSheetBehaviour behaviour = BottomSheetBehaviour.from(bottomSheet);
                // 强制设置为完全展开状态,自适应内容高度
                behaviour.setState(BottomSheetBehaviour.STATE_EXPANDED);
                // 禁用拖动,防止用户改变BottomSheet高度
                behaviour.setDraggable(false);
                
                behaviour.addBottomSheetCallback(new LockableBottomSheetBehavior.BottomSheetCallback() {
                    @Override
                    public void onStateChanged(@NonNull View bottomSheet, int newState) {
                        if (newState == CustomBottomSheetBehaviour.STATE_DRAGGING) {
                            behaviour.setState(CustomBottomSheetBehaviour.STATE_EXPANDED);
                        } else if (newState == CustomBottomSheetBehaviour.STATE_EXPANDED) {
                            if (behaviour instanceof LockableBottomSheetBehavior) {
                                ((LockableBottomSheetBehavior) behaviour).setLocked(true);
                            }
                        }
                    }

                    @Override
                    public void onSlide(@NonNull View bottomSheet, float slideOffset) {
                        // 移除不必要的触摸监听,避免干扰布局高度计算
                    }
                });
            }
        }
    });

    // 原有返回键监听逻辑保持不变
    if (from == Constants.ADD_FRIEND_CONFIRMATION || from == Constants.DELETE_EXPENSE) {
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK
                        && event.getAction() == KeyEvent.ACTION_UP) {
                    if (from == Constants.ADD_FRIEND_CONFIRMATION) {
                        AddFriendConfirmationFragment addFriendConfirmationFragment = 
                            (AddFriendConfirmationFragment) getChildFragmentManager()
                                .findFragmentByTag("addFriendConfirmationFragment");
                        if (addFriendConfirmationFragment != null 
                            && addFriendConfirmationFragment.status == 1) {
                            dismissDialog();
                            return true;
                        }
                    } 
                    return false;
                }
                return false;
            }
        });
    }
    return dialog;
}

3. 修复Base布局的可见性

确保BaseBottomsheetDialogFragment的FrameLayout初始可见性为visible,保证子Fragment布局能正确计算高度:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rootlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/framelayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="visible">
    </FrameLayout>
</RelativeLayout>

4. 统一依赖库版本

检查新项目build.gradle中Material Components的版本,确保和旧项目一致,避免版本差异导致的行为变化:

implementation 'com.google.android.material:material:对应版本号'

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 22:24:54