android - Android:BottomSheetDialog 未使用 ConstraintLayout 完全展开
问题描述
我正在为我的布局使用ConstraintLayout。我正在创建一个BottomSheetDialog并将布局设置为它,它在顶部使用ConstraintLayout 。
注意:如果我使用 LinearLayout 一切正常,并且 BottomSheetDialog 具有适当的高度,但是当我使用 ConstraintLayout 时,它只显示一个选项。
请在我使用 ConstraintLayout 时找到下面的屏幕截图:
我的 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);
}
});
在我的布局中,如果我用LinearLayout替换ConstraintLayout ,那么它可以正常工作并占据全高。如果我做错了什么,请告诉我。
我已经尝试了这些解决方案,但没有任何效果:
- 将 BottomSheetDialogFragment 的状态设置为展开
- BottomSheetDialogFragment - 如何设置扩展高度(或最小顶部偏移)
- Android BottomSheetDialogFragment 没有完全展开
任何帮助将不胜感激。谢谢!
解决方案
你没有限制所有的观点——第一个TextView
没有任何限制。您添加的约束未正确指定以正确按垂直顺序显示视图。我建议创建所有视图的垂直链,并将父级ConstraintLayout's
layout_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>
作为旁注,不建议使用文档建议match_parent
的 a 内的任何视图:ConstraintLayout
重要提示:不建议将 MATCH_PARENT 用于 ConstraintLayout 中包含的小部件。可以通过使用 MATCH_CONSTRAINT 来定义类似的行为,并将相应的左/右或上/下约束设置为“父”。
推荐阅读
- virtual-machine - 由 Jack 中的 getter mwthod 导致 VMEmulator 中的“段空间不足”错误
- r - 如何解决我在使用 inner_join 函数时遇到的这个错误?
- javascript - 每次单击按钮时如何创建一个新的 DIV?
- ios - 更新 SwiftUI 视图大小以匹配图像
- java - 如何在java中添加2个文本文件的内容
- android - Firebase startAt 在检索数据时返回 null
- azure - 从 Azure DevOps yaml 设置应用服务应用程序设置
- python - AttributeError:类型对象没有属性(*args 有问题?)
- c# - “找不到类型或命名空间‘BlobTrigger’”
- python - RuntimeError:模型类 users.models.Profile 未声明显式 app_label 并且不在 INSTALLED_APPS 中的应用程序中