【发布时间】:2019-12-17 02:46:43
【问题描述】:
我有一个自定义对话框,里面有元素。顶部有一个标题,一个滚动视图,里面有一个文本视图(这是文本所在的位置)和下面的两个按钮。我在这里想要实现的是扩展滚动视图(以及随之而来的对话框)以适应将要呈现的文本,并且当对话框达到某个限制(例如 500dp 高度)时,扩展将停止和任何未显示的额外文本,用户只需滚动即可查看。
我尝试过 wrap_content,但它会扩展到某个点,然后它会从底部打破约束,并且滚动视图位于按钮下方。
约束尺寸:
android:layout_width="500dp" android:layout_height="500dp"
标题文本视图属性:
android:id="@+id/title"
android:layout_width="500dp"
android:layout_height="wrap_content"
android:layout_marginTop="40"
android:paddingStart="20"
android:text="Title"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
滚动视图属性:
android:id="@+id/scrollview"
android:layout_width="500dp"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:fillViewport="true"
app:layout_constraintBottom_toTopOf="@+id/cancel"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/title"
滚动视图内的文本视图:
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
取消按钮:
android:id="@+id/cancel"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_marginStart="100dp"
android:layout_marginBottom="40dp"
android:text="cancel"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
确认按钮:
android:layout_width="150dp"
android:layout_height=50dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="40dp"
android:text="confirm"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
当我从 java 显示它时:
View layoutView = getLayoutInflater().inflate(R.layout.dialog_layout,null);
TextView text = layoutView.findViewById(R.id.text);
text.setText("INSERT TEXT HERE TO VIEW DIALOG BEHAVIOUR");
final Dialog dialog = new Dialog(this);
dialog.setContentView(layoutView);
dialog.create();
dialog.show();
有什么我想念的吗?我尝试将 0dp 放在滚动视图的约束布局下,但仍然没有。请帮忙
*编辑:* 基于这个问题Android ScrollView set height for displayed content 和这个答案就在这里Android: why is there no maxHeight for a View?
他们以某种方式设法做到了动态。但是这种方法不适用于对话框,因为通过将线性布局设置为高度,对话框的大小不是动态的。这甚至可能吗??
【问题讨论】:
标签: android layout dynamic dialog