Android如何设置对话框大小 ScrollView嵌套ConstraintLayout match_parent不显示问题
可滚动对话框布局显示异常解决方案
问题根因
布局无法正常显示是属性冲突导致的:
- ScrollView开启了
android:fillViewport="true",会强制拉伸内部子View填满ScrollView的可视区域 - 内部ConstraintLayout设置
wrap_content高度的同时,子View使用了0dp(即match_constraint)的高度配置,在父布局没有固定高度的前提下,ConstraintLayout无法完成正常测量,最终导致布局加载失败。
解决方案
不需要调整嵌套结构,仅修改少量属性即可实现「ConstraintLayout宽度match_parent、高度wrap_content」的需求:
- 保持ScrollView的固定宽高配置不变,根据业务需求决定是否保留
fillViewport属性:- 如果内容大概率小于400dp,希望按钮固定在对话框底部,保留
fillViewport="true" - 如果仅需要内容超过400dp时滚动,内容不足时从顶部排列,直接删除这个属性即可
- 如果内容大概率小于400dp,希望按钮固定在对话框底部,保留
- 将ConstraintLayout的宽度改为
match_parent,高度保持wrap_content - 调整内部子View的高度配置:
- 两个TextView的
android:layout_height默认改为wrap_content - 如果保留了
fillViewport属性且需要按钮固定在底部,给dialogDescription额外添加app:layout_constraintHeight_default="wrap"属性,同时补全dialogDescription底部到按钮顶部的约束即可 - 如果不需要按钮固定底部,删除按钮的
app:layout_constraintBottom_toBottomOf="parent"属性即可
- 两个TextView的
修改后核心代码示例
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="700dp" android:layout_height="400dp" android:background="@color/main_blue" android:fadeScrollbars="false" android:fillViewport="true" android:scrollbarAlwaysDrawVerticalTrack="true" android:scrollbarFadeDuration="0" android:scrollbarThumbVertical="@drawable/thumb_dialog" android:scrollbarTrackVertical="@drawable/track_dialog"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/dialogTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="@dimen/margin_xxxxlarge" android:layout_marginTop="@dimen/margin_xxxxlarge" android:layout_marginEnd="@dimen/margin_xxxxlarge" android:text="@string/dialog_title" android:textAllCaps="true" android:textColor="@color/main_red" android:textSize="@dimen/text_size_xl" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/dialogDescription" android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginStart="@dimen/margin_xxxxlarge" android:layout_marginTop="@dimen/margin_xxxxlarge" android:layout_marginEnd="@dimen/margin_xxxxlarge" android:text="@string/dialog_message" android:textSize="@dimen/text_size_m" app:layout_constraintHeight_default="wrap" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/dialogTitle" app:layout_constraintBottom_toTopOf="@+id/agreeButton"/> <Button android:id="@+id/agreeButton" style="@style/RedButton" android:layout_width="188dp" android:layout_height="49dp" android:layout_marginTop="@dimen/margin_xxxxlarge" android:layout_marginBottom="@dimen/margin_xxxxlarge" android:background="@drawable/red_button" android:text="@string/dialog_button_text" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="@+id/dialogTitle" /> </androidx.constraintlayout.widget.ConstraintLayout> </ScrollView>
布局预览

内容的提问来源于stack exchange,提问作者Jakub Słowiński
相关产品推荐
相关产品推荐

