NestedScrollView 没有在 AlertDialog 内扩展高度

Posted

技术标签:

【中文标题】NestedScrollView 没有在 AlertDialog 内扩展高度【英文标题】:NestedScrollView not expanding in height inside AlertDialog 【发布时间】:2020-05-10 03:28:50 【问题描述】:

我目前在警报对话框中使用滚动视图,并且需要滚动视图增加高度,直到对话框达到其最大默认高度。我解释起来有点困难,所以我附上了一个插图来帮助。希望是的。

我遇到的问题是滚动视图的高度没有增长,即使我删除 app:layout_constraintBottom_toTopOf="@id/linearLayout4",它也会增长,但底部部分将被按钮布局部分隐藏。 这是我当前的代码:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/filter_layout"
android:layout_
android:layout_
android:background="@drawable/dialog_box"
android:minHeight="300dp"
android:elevation="12dp">

<TextView
    android:id="@+id/filter_header"
    android:layout_
    android:layout_
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:text="@string/filter"
    style="@style/element_header"
    android:textColor="@color/textColorDark"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/filter_reset_btn"
    android:layout_
    android:layout_
    android:background="?attr/selectableItemBackgroundBorderless"
    android:text="@string/reset"
    style="@style/text_info_nBold"
    android:textSize="14sp"
    android:textColor="@color/textColorDark"
    app:layout_constraintBottom_toBottomOf="@+id/filter_header"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="@+id/filter_header" />

<LinearLayout
    android:layout_
    android:layout_
    android:orientation="vertical"
    app:layout_constraintBottom_toTopOf="@id/linearLayout4"
    app:layout_constraintTop_toBottomOf="@id/filter_header"
    app:layout_constraintVertical_bias="0.0">

    <androidx.core.widget.NestedScrollView
        android:id="@+id/filter_scroll"
        android:layout_
        android:layout_
        android:fillViewport="true"
        android:layout_marginTop="16dp"
        android:scrollbarFadeDuration="1000">

        <!-- VIEWS INSIDE HERE -->

    </androidx.core.widget.NestedScrollView>

</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout4"
    android:layout_
    android:layout_
    android:background="@drawable/dialog_bottombar_layout"
    android:orientation="horizontal"
    android:padding="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent">

    <Button
        android:id="@+id/dialog_secondary_button"
        style="@style/low_emphasis_btn"
        android:layout_
        android:layout_marginEnd="8dp"
        android:layout_weight="1"
        android:backgroundTint="@color/textColorDark"
        android:text="@string/dialog_cancel"
        android:textColor="@color/textColorDark"
        android:visibility="visible" />

    <Button
        android:id="@+id/dialog_primary_button"
        style="@style/high_emphasis_btn"
        android:layout_
        android:layout_weight="1"
        android:text="@string/apply" />

</LinearLayout>

任何帮助将不胜感激 :)

【问题讨论】:

“操作对话框”对我来说是新的。你的意思是警报对话框吗?用于显示对话框的代码也可能会有所帮助。 哦。对不起,我实际上犯了一个错误,它的 AlertDialog。谢谢你为我指出这一点 【参考方案1】:

警报对话框倾向于将其内容包装起来,或者可以强制全屏显示。优化屏幕空间之间的大小需要一些工作,但这并非不可能。

一种方法是让系统布置警报对话框,但在显示之前,使用 ViewTreeObserver.OnGlobalLayoutListener 来检查对话框的最终大小。在布局监听器中,可以调整对话框的大小以适应滚动视图的内容,如果滚动视图内容对于屏幕来说太大,则可以调整为全屏。

这是一个演示应用程序,展示了如何做到这一点。代码中的注释解释更多。

MainActivity.kt

class MainActivity : AppCompatActivity(), View.OnClickListener 

    override fun onCreate(savedInstanceState: Bundle?) 
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    

    override fun onClick(v: View) 
        val text = when (v.id) 
            R.id.customDialogShort -> getString(R.string.short_string)
            R.id.customDialogMedium -> getString(R.string.lorem_medium)
            else -> getString(R.string.lorem_long)
        

        // Specifying the viewGroup as a parent to the inflater makes no difference.
        val dialogView = LayoutInflater.from(v.context).inflate(R.layout.con_custom_view, null, false) as ConstraintLayout
        (dialogView.findViewById(R.id.textView) as TextView).text = text

        val alertDialog = AlertDialog.Builder(this).setView(dialogView).create()
        val decorView = alertDialog.window!!.decorView
        decorView.setBackgroundResource(R.drawable.alert_dialog_background)

        // We need a layout pass to determine how big everything is and needs to be. Place a hook
        // at the end of the layout process to examine the layout before display.
        decorView.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener 
            override fun onGlobalLayout() 
                decorView.viewTreeObserver.removeOnGlobalLayoutListener(this)

                // Find out how much of the scrolling view is usable by its child.
                val scrollingView: NestedScrollView = decorView.findViewById(R.id.filter_scroll)
                val scrollingViewPadding = scrollingView.paddingTop + scrollingView.paddingBottom
                val scrollingUsableHeight = scrollingView.height - scrollingViewPadding

                // If the child view fits in the scrolling view, then we are done.
                val childView = scrollingView.getChildAt(0)
                if (childView.height <= scrollingUsableHeight) 
                    return
                

                // Child doesn't currently fit in the scrolling view. Resize the top-level
                // view so the child either fits or is forced to scroll because the maximum
                // height is reached. First, find out how much space is allowed by the decor view.
                val displayRectangle = Rect()
                decorView.getWindowVisibleDisplayFrame(displayRectangle)
                val decorViewPadding = decorView.paddingTop + decorView.paddingBottom
                val decorUsableHeight = displayRectangle.height() - decorViewPadding - scrollingViewPadding

                // Compute the height of the dialog that will 100% fit the scrolling content and
                // reduce it if it won't fit in the maximum allowed space.
                val heightToFit = dialogView.height + childView.height - scrollingUsableHeight
                dialogView.minHeight = min(decorUsableHeight, heightToFit)
            
        )
        var buttonOk: Button = dialogView.findViewById(R.id.dialog_primary_button)
        buttonOk.setOnClickListener  alertDialog.dismiss() 
        buttonOk = dialogView.findViewById(R.id.dialog_secondary_button)
        buttonOk.setOnClickListener  alertDialog.dismiss() 
        alertDialog.show()
    

activity_main.xml

<LinearLayout 
    android:id="@+id/parent"
    android:layout_
    android:layout_
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/customDialogShort"
        android:layout_
        android:layout_
        android:onClick="onClick"
        android:text="Short text" />

    <Button
        android:id="@+id/customDialogMedium"
        android:layout_
        android:layout_
        android:onClick="onClick"
        android:text="Medium text" />

    <Button
        android:id="@+id/customDialogLong"
        android:layout_
        android:layout_
        android:onClick="onClick"
        android:text="Long text" />

</LinearLayout>

con_custom_view 警报对话框的自定义布局。

    <TextView
        android:id="@+id/filter_header"
        android:layout_
        android:layout_
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:text="@string/filter"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/filter_reset_btn"
        android:layout_
        android:layout_
        android:background="?attr/selectableItemBackgroundBorderless"
        android:text="@string/reset"
        android:textSize="14sp"
        app:layout_constraintBottom_toBottomOf="@+id/filter_header"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/filter_header" />

    <androidx.core.widget.NestedScrollView
        android:id="@+id/filter_scroll"
        android:layout_
        android:layout_
        android:layout_marginTop="16dp"
        android:padding="16dp"
        android:scrollbarFadeDuration="1000"
        app:layout_constraintBottom_toTopOf="@id/linearLayout4"
        app:layout_constraintTop_toBottomOf="@id/filter_header">

        <TextView
            android:id="@+id/textView"
            android:layout_
            android:layout_
            tools:text="@string/lorem_long" />

    </androidx.core.widget.NestedScrollView>

    <LinearLayout
        android:id="@+id/linearLayout4"
        android:layout_
        android:layout_
        android:orientation="horizontal"
        android:padding="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <Button
            android:id="@+id/dialog_secondary_button"
            android:layout_
            android:layout_
            android:layout_marginEnd="8dp"
            android:layout_weight="1"
            android:text="@string/dialog_cancel"
            android:visibility="visible" />

        <Button
            android:id="@+id/dialog_primary_button"
            android:layout_
            android:layout_
            android:layout_weight="1"
            android:text="@string/apply" />

    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

我正在使用 java,还没有过渡到 kotlin,所以我发现你的代码有点挑战性。这里的“dialogView.minHeight = min(decorUsableHeight, heightToFit)”是什么意思你是在调用 Math.min 函数吗? 请分享您的对话框布局的 xml 文件 @jeanlucrotolo Here 是 Java 版本。我在答案中发布了自定义布局。 很抱歉,它不像视频中显示的那样工作。调用 dialog.setMinimumHeight() 后布局没有变化 @jeanlucrotolo 在ConstraintLayout中设置最小高度是dialogView.setMinHeight()

以上是关于NestedScrollView 没有在 AlertDialog 内扩展高度的主要内容,如果未能解决你的问题,请参考以下文章

NestedScrollView,RecyclerView

如何禁用 NestedScrollView&CollapsingToolbarLayout 的滚动,例如当下面没有更多内容时?

如何使用里面的 ListView 滚动 NestedScrollView?

nestedscrollview 内的两个 Recyclerview

以编程方式滚动到 NestedScrollView 的顶部

NestedScrollView嵌套ListView时只显示一行的解决方法