有没有一种方法可以使自定义对话框扩展到特定限制?

时间:2019-08-09 11:02:01

标签: android layout dynamic dialog

我有一个自定义对话框,其中包含元素。顶部有一个标题,内部有textview的滚动视图(这是文本所在的位置),下面有两个按钮。我在这里想要实现的是扩展滚动视图(以及对话框以及它)以适应将要显示的文本,并且当对话框达到某个限制(例如500dp高度)时,扩展将停止并显示未显示的其他任何文字,用户只需滚动即可查看。

我尝试了wrap_content,但是它会扩展到某个点,然后从底部打破约束,并且滚动视图进入按钮下方。

约束变暗:

android:layout_width="500dp" android:layout_height="500dp"

标题textview属性:

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"

scrollview属性:

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"

textview滚动视图内:

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置于scrollview的约束布局下,但仍然一无所有。请帮助

*编辑:* 基于这个问题Android ScrollView set height for displayed content和这个答案Android: why is there no maxHeight for a View?

他们以某种方式设法做到了动态。但是,此方法不适用于对话框,因为通过将线性布局设置为高度,对话框的大小不是动态的。这甚至有可能吗?

2 个答案:

答案 0 :(得分:0)

尝试一下:

通过编程将maxHeight设置为所需的限制:

text.setMaxHeight(您的限制);

将maxHeight设置为scrollView,然后将textView设置为match_parent

答案 1 :(得分:0)

最后,在不扩展scrollview类和自己实现此行为的情况下,可以实现此目的的另一种解决方法……此处的帖子:Android ScrollView set height for displayed content

在展开对话框和动态设置高度之前进行检查。这样就解决了这个问题,对看似微不足道的东西有点太麻烦了。不过,您必须将滚动视图包装成线性布局,如我编辑的说明

中所述
sv.measure(0, 0);
if (nsv.getMeasuredHeight() > 200) {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                                      ViewGroup.LayoutParams.MATCH_PARENT, 200);
        sv.setLayoutParams(lp);
}

如果还有另一种更合适的方法,我全神贯注。