Android Bottomsheet TextView 高度在第一次扩展时未调整

标签 android android-design-library bottom-sheet

TL;DR:Bottomsheet 中的 TextView 第一次展开 Bottomsheet 时未显示换行的多行文本,但在展开后自行调整崩溃了。

所以我正在使用 design-23.2.1 库中的 Bottomsheet。

我的布局文件如下所示:

<android.support.design.widget.CoordinatorLayout>

    ......

    <LinearLayout
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:behavior_peekHeight="@dimen/bottom_sheet_peek_height"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior"/>
</android.support.design.widget.CoordinatorLayout>

Bottomsheet 的内容基本上是一个列表:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView 
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:singleLine="false" />
    ...
</LinearLayout>

问题是每当 Bottomsheet 设置为 STATE_EXPANDED 第一次时,TextView 是单行并且文本被换行,行尾没有省略号 ...

然后在设置为STATE_COLLAPSED后,TextView的高度就可以了,并且可以正常多行显示。

我知道高度重新布局发生在设置为 STATE_COLLAPSED 之后,因为我将它从折叠状态中滑出并且多线已经存在。

提供了解决方法 here .我跟着它添加了这个:

bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
    @Override
    public void onStateChanged(@NonNull View bottomSheet, int newState) {
        if (newState == BottomSheetBehavior.STATE_EXPANDED) {
            bottomSheetBehavior.onLayoutChild(coordinatorLayout,
                                            bottomSheetView,
                                            ViewCompat.LAYOUT_DIRECTION_LTR);
        }
    }
    ........
}

它确实在 Bottomsheet 第一次展开时重新调整了高度。然而它是在展开动画完成后突然发生的。

有没有办法像 Google Map 一样调整展开动画之前的高度?

更新

我发现这个问题是因为我在展开之前将Bottomsheet设置为STATE_COLLAPSED。如果没有设置,那么问题就消失了,第一次就正确调整了高度。

现在我的问题是:为什么在展开前将其设置为 STATE_COLLAPSED 会导致该问题?

最佳答案

如果出于某种原因您仍然必须使用旧的支持库,这里是解决方法。

mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(@NonNull final View bottomSheet, int newState) {
            bottomSheet.post(new Runnable() {
                @Override
                public void run() {
                    //workaround for the bottomsheet  bug
                    bottomSheet.requestLayout();
                    bottomSheet.invalidate();
                }
            });
        }

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

关于Android Bottomsheet TextView 高度在第一次扩展时未调整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36249844/

相关文章:

android - 在BottomSheetFragment中将onCancel重写为setState为COLLAPSED

android - setOutputFormat 在无效状态下调用 : 4 (where and why)

android - VisibilityAwareImageButton 用法

android - 如何根据谷歌新指南实现底部导航选项卡

android - 如何使用 ADT 插件将 "Android Design Support Library"添加到 Eclipse?

android - 谷歌地图触摸 Bottom Sheet 对话框

android - 使用 RecyclerView 时未显示 BottomSheet

android - 错误-从 Android Webview 中的 URL 加载 PDF

java - 如何解决布局中更多数字的 Editext 问题并从中获取值?

android - 使用 addContentView 向我的主视图添加一个新的 subview