如何根据较大的高度设置可变高度,无论是match_constraint还是wrap_content

时间:2019-04-23 18:33:37

标签: android kotlin android-constraintlayout android-scrollview

我有一个ScrollView-> ConstraintLayout,它基本上占据了整个屏幕。我在这里在屏幕上布置了几个项目,还有一个菜单并没有占据整个视图。菜单将交换出约束在约束布局底部的容器中的片段。现在我遇到的问题是我似乎找不到一种将容器限制在页面底部并将其高度设置为match_constraint的方法,并且也无法根据容器内部片段高度的大小来增大容器的大小容器,然后使页面可滚动。我可以设置容器来包装内容,如果有足够的内容可以很好地工作,但是如果内容小于屏幕的其余部分,则它不会填满屏幕,也不会像我一样将容器背景绘制到底部想。

val tc = super.requireContext()

        val scrollView = ScrollView(tc)
        scrollView.id = View.generateViewId()
        scrollView.isFillViewport = true
        scrollView.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)

        val thisLayout = ConstraintLayout(tc)
        thisLayout.id = View.generateViewId()
        thisLayout.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        scrollView.addView(thisLayout)

        val menuLayout = ManageableMenu(tc,listOf(
            ManageableMenu.ManageableMenuItem("Item 1", Fragment()),
            ManageableMenu.ManageableMenuItem("Item 2", Fragment()),
            ManageableMenu.ManageableMenuItem("Item 3", Fragment())
        ))
        menuLayout.listener = this
        val menuLayoutConstraint = menuLayout.layoutParams as ConstraintLayout.LayoutParams
        menuLayoutConstraint.topToBottom = thisLayout.id
        menuLayoutConstraint.startToStart = thisLayout.id
        menuLayoutConstraint.endToEnd = thisLayout.id
        menuLayout.setPadding(0, 0, 0 , convertToDP(10, tc))
        thisLayout.addView(menuLayout)

        val fragmentContainer =  FrameLayout(tc)
        fragmentContainer.id = View.generateViewId()
        fragmentContainer.setBackgroundColor(Color.parseColor("#262626"))
        val fragmentContainerLayout = ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_CONSTRAINT, ConstraintLayout.LayoutParams.MATCH_CONSTRAINT)
        fragmentContainerLayout.topToBottom = menuLayout.id
        fragmentContainerLayout.leftToLeft = thisLayout.id
        fragmentContainerLayout.rightToRight = thisLayout.id
        fragmentContainerLayout.bottomToBottom = thisLayout.id
        thisLayout.addView(fragmentContainer, fragmentContainerLayout)

现在,它完全可以正常工作了,没有任何内容。但是,当我向fragmentContainer添加一个视图,该视图比容器的高度受限制的高时,我无法滚动。

当我将fragmentContainers的高度设置为“ WRAP_CONTENT”时,当内容大于屏幕其余部分,滚动以及所有内容时,它可以完美工作。但是,如果没有或没有内容。容器背景没有延伸到屏幕底部,看起来像垃圾。

必须有某种方法使这项工作正确吗?我在LayoutParams上尝试了许多不同的属性,例如minConstrainedHeight和诸如此类,但没有任何效果。预先感谢!

0 个答案:

没有答案
相关问题