【问题标题】:Is there a way to make custom Dialog expand up to a certain limit?有没有办法让自定义对话框扩展到一定的限制?
【发布时间】: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


    【解决方案1】:

    试试这个:

    以编程方式将 maxHeight 设置为您想要的限制:

    text.setMaxHeight(你的限制);

    或者

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

    【讨论】:

    • 查看说明中的编辑,了解为什么我们的两个答案都不适用
    【解决方案2】:

    最后,在不扩展 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);
    }
    

    如果有其他更合适的方法,我会全力以赴。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-07
      • 1970-01-01
      • 2020-12-27
      • 1970-01-01
      相关资源
      最近更新 更多