代码之家  ›  专栏  ›  技术社区  ›  Zonico

尽管设置了维度,但constraintlayout中的自定义视图最终的大小为0

  •  1
  • Zonico  · 技术社区  · 7 年前

    我有一个自定义视图类( EmptyTableView ),通常工作正常。但是,在程序的某一点上,当我将此视图置于 constraintlayout 的中心时,该视图突然变为不可见。进一步的调查显示,视图定位在 (0,0) with the size (0 x 0)
    在my onmeasure()内的 measurespecs inside my onmeasure() are exactly 0 。我不知道这意味着什么,但是有人说当你搞砸测量模式时会发生这种情况,尽管我不知道为什么会在我的视图中发生。 这是源代码。

    在我的应用程序中,我的视图位于一个名为“根”的 ConstraintLayout Called Root中,它的位置可以通过一个 DragandDrop Procedure.
    我的计划是让视图对所有4个边都有约束,而位置是通过约束的偏差来控制的。当我开始拖动视图时,左、右约束A被删除,位置由 setx()控制 and sety() 。使用此方法完成此操作(Kotlin):

    fun constraintset.PrepareConstraintsForDrag(Table:View){
    清除(table.id)
    连接(table.id,constraintset.start,constraintset.parent_id,constraintset.start,table.left)
    连接(table.id,constraintset.top,constraintset.parent_id,constraintset.top,table.top)
    }
    

    然后,当我结束dragandrop时,我从我的拖放回调中调用以下内容,以便将视图布局移动到x和y,然后调用restorerebiases()再次连接所有4个面:

    dragevent.action_drag_ended->。{
    shadowTouchPoint=空//只是拖动的内部指示器
    val draggedview=event.localstate作为视图
    带(DraggedView){
    可见性=视图。可见
    
    rootconstraints.clone(根)
    setMargin(id,constraintset.start,x.toInt())
    setMargin(id,constraintset.top,y.toint())
    rootconstraints.applyto(根)
    
    平移x=0f
    平移Y=0F
    }
    rootconstraints.clone(根)
    rootConstraints.restoreBiales(draggedView,options_-guide,root.height.tofloat())
    rootconstraints.applyto(根)
    }
    

    restorebiasses():。

    fun constraintset.restorebiales(table:view,sideguide:guideline,height:float){
    val xbias=table.x/(sideguide.left-table.width).tofloat()。
    Val Ybias=表.y/(高度-表.height)
    
    中心(table.id,constraintset.parent_id,constraintset.start,0,
    sideguide.id,constraintset.start,0,xbias)
    中心(table.id,constraintset.parent_id,constraintset.top,0,
    constraintset.parent_id,constraintset.bottom,0,ybias)
    }
    

    整个代码都在github上。PrepareConstraintsForDrag()andPrepareBiases()are located inTableScene.kt.>code>。tabbleplan/constructEmptyTablePlan.kt中的回调andEmptyTableView.ktin thetableplandirectory.

    这就是它在活动中的大致外观:

    =ConstraintLayout突然间,视野变得看不见了。进一步调查表明,视图定位在(0, 0)尺寸(0 x 0).
    这个measureSpecs在我的onMeasure()EXACTLY 0.我不知道那是什么意思,但是
    someone虽然我不知道在我看来为什么会发生这种情况,但如果你搞砸了测量模式,就会发生这种情况。 这是源代码。

    在我的应用程序中,我的视图位于约束布局称为根,它的位置可以通过DragAndDrop程序。
    我的计划是让视图对所有4个边都有约束,而位置是通过约束的偏差来控制的。当我开始拖动视图时,左右约束A被删除,位置由setX()setY().使用此方法完成此操作(Kotlin):

    fun ConstraintSet.prepareConstraintsForDrag(table: View) {
            clear(table.id)
            connect(table.id, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, table.left)
            connect(table.id, ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, table.top)
        }
    

    然后,当我结束拖地车,我从拖放回调中调用以下内容,以便将视图布局移动到x和y,然后调用restorebiases()再次连接所有4个面:

    DragEvent.ACTION_DRAG_ENDED -> {
                        shadowTouchPoint = null // Just an intern indicator for the drag
                        val draggedView = event.localState as View
                        with(draggedView) {
                            visibility = View.VISIBLE
    
                            rootConstraints.clone(root)
                            setMargin(id, ConstraintSet.START, x.toInt())
                            setMargin(id, ConstraintSet.TOP, y.toInt())
                            rootConstraints.applyTo(root)
    
                            translationX = 0f
                            translationY = 0f
                        }
                        rootConstraints.clone(root)
                        rootConstraints.restoreBiases(draggedView, options_guide, root.height.toFloat())
                        rootConstraints.applyTo(root)
                    }
    

    restorebiases():

    fun ConstraintSet.restoreBiases(table: View, sideGuide: Guideline, height: Float) {
            val xBias = table.x / (sideGuide.left - table.width).toFloat()
            val yBias = table.y / (height - table.height)
    
            center(table.id, ConstraintSet.PARENT_ID, ConstraintSet.START, 0,
                    sideGuide.id, ConstraintSet.START, 0, xBias)
            center(table.id, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 0,
                    ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0, yBias)
        }
    

    整个代码都打开了GitHub.prepareConstraintsForDrag()prepareBiases()位于TableScene.kt.回拨入tabblePlan/ConstructEmptyTablePlan.ktEmptyTableView.kttablePlan目录。

    这就是它在活动内部的大致情况:

    After starting the activity While dragging (the upper right is the dragshadow, there was my thumb while taking the picture, in the middle the actual view which I don't hide for debugging

    1 回复  |  直到 7 年前
        1
  •  2
  •   Cheticamp    7 年前

    升级至1.1.2版 ConstraintLayout 以下内容:

    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    

    我想可能有问题 center() 在此版本之前。我不认为这能解决你的问题,但它可能会为你找到潜在问题扫清道路。换句话说,我认为你会在屏幕上看到一些不同的东西。

    我可以说你的指导方针 options_guide 以某种方式设置为屏幕的宽度。也许这是目的,但如果不是,我会研究如何设置和使用这个准则。