安卓:使用ConstraintLayout时,BottomSheetDialog无法完全展开

4
我正在使用ConstraintLayout来布局。我正在创建一个BottomSheetDialog,并将其布局设置为使用ConstraintLayout在顶部的布局。

注意:如果我使用LinearLayout,则一切正常,BottomSheetDialog会采用适当的高度,但是当我使用ConstraintLayout时,它只显示一个选项。

请查看以下截图,当我使用ConstraintLayout时:

enter image description here

我的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/white">

    <TextView
        android:id="@+id/tv_other_methods"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/ripple"
        android:gravity="center_horizontal"
        android:padding="@dimen/dp_12"
        android:text="@string/from_other_methods"
        android:textColor="@color/blue"
        android:textSize="@dimen/sp_17" />

    <View
        android:id="@+id/view_other_methods"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dip_1"
        android:background="@color/light_grey"
        app:layout_constraintBottom_toBottomOf="@id/tv_other_methods" />

    <TextView
        android:id="@+id/tv_FromChinaUnionPay"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/ripple"
        android:gravity="center_horizontal"
        android:padding="@dimen/dp_12"
        android:text="@string/from_china_union_pay"
        android:textColor="@color/blue"
        android:textSize="@dimen/sp_17"
        app:layout_constraintBottom_toBottomOf="@id/view_other_methods"
        app:layout_constraintTop_toTopOf="@id/view_FromChinaUnionPay" />

    <View
        android:id="@+id/view_FromChinaUnionPay"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dip_1"
        android:background="@color/light_grey"
        app:layout_constraintBottom_toBottomOf="@id/tv_FromChinaUnionPay" />

    <TextView
        android:id="@+id/tv_Cancel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/ripple"
        android:gravity="center_horizontal"
        android:padding="@dimen/dp_12"
        android:text="@string/cancel"
        android:textColor="@color/blue"
        android:textSize="@dimen/sp_17"
        app:layout_constraintTop_toBottomOf="@+id/view_FromChinaUnionPay" />
</android.support.constraint.ConstraintLayout>

我的BottomSheetDialog的Java代码:

final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(mContext);
            bottomSheetDialog.setContentView(R.layout.dialog_web_view_options);
            // dialog.setCancelable(false);
            final Window window = bottomSheetDialog.getWindow();
            assert window != null;
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            window.setLayout(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT);
            window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
            window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
            window.getDecorView().setPadding(20, 0, 20, 0);

            bottomSheetDialog.setOnShowListener(new DialogInterface.OnShowListener() {
                @Override
                public void onShow(DialogInterface dialog) {
                    FrameLayout bottomSheet = bottomSheetDialog.findViewById(android.support.design.R.id.design_bottom_sheet);
                    BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
                    behavior.setSkipCollapsed(true);
                    behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                }
            });

在我的布局中,如果我将ConstraintLayout替换为LinearLayout,那么它可以正常工作并占据满整个高度。请告诉我是否有哪里做错了。
我尝试了以下这些解决方案,但都没有起作用: 欢迎提供任何帮助。谢谢!

1
为什么是-1?如果有人给我负面评价,他/她必须解释原因,这样我才能了解我的问题出在哪里并加以改进。 - Däñish Shärmà
1个回答

1
你还没有对所有视图进行约束 - 第一个 TextView 没有任何约束。你添加的约束没有正确指定以垂直顺序正确显示视图。我建议创建一个包含所有视图的垂直链,父级 ConstraintLayoutlayout_height 设置为 wrap_content。XML 应该类似于这样:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white">

    <TextView
        android:id="@+id/tv_other_methods"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/ripple"
        android:gravity="center_horizontal"
        android:padding="@dimen/dp_12"
        android:text="@string/from_other_methods"
        android:textColor="@color/blue"
        android:textSize="@dimen/sp_17"
        app:layout_constraintBottom_toTopmOf="@id/view_other_methods"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/view_other_methods"
        android:layout_width="0dp"
        android:layout_height="@dimen/dip_1"
        android:background="@color/light_grey"
        app:layout_constraintBottom_toTopOf="@id/tv_FromChinaUnionPay"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv_other_methods" />

    <TextView
        android:id="@+id/tv_FromChinaUnionPay"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/ripple"
        android:gravity="center_horizontal"
        android:padding="@dimen/dp_12"
        android:text="@string/from_china_union_pay"
        android:textColor="@color/blue"
        android:textSize="@dimen/sp_17"
        app:layout_constraintBottom_toTopOf="@id/view_FromChinaUnionPay"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/view_other_methods" />

    <View
        android:id="@+id/view_FromChinaUnionPay"
        android:layout_width="0dp"
        android:layout_height="@dimen/dip_1"
        android:background="@color/light_grey"
        app:layout_constraintBottom_toTopOf="@id/tv_Cancel"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv_FromChinaUnionPay" />

    <TextView
        android:id="@+id/tv_Cancel"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/ripple"
        android:gravity="center_horizontal"
        android:padding="@dimen/dp_12"
        android:text="@string/cancel"
        android:textColor="@color/blue"
        android:textSize="@dimen/sp_17"
        app:layout_constraintTop_toBottomOf="@+id/view_FromChinaUnionPay"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.constraint.ConstraintLayout>

作为补充说明,不建议在 ConstraintLayout 中使用 match_parent 来设置任何视图,因为文档中指出:

重要提示:不建议在 ConstraintLayout 中使用 MATCH_PARENT 来设置小部件。可以通过将相应的左/右或上/下约束设置为“parent”来定义类似的行为。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接