GridLayout spitting out “inconsistent constraint” debug-level logs

前端 未结 4 1928
忘掉有多难
忘掉有多难 2021-01-01 11:30

I\'ve been using GridLayout for a few weeks now and I\'ve noticed that when I call

gridLayout.requestLayout()

it spits out the following de

相关标签:
4条回答
  • 2021-01-01 11:33

    From the GridLayout source:

    Bellman-Ford variant - modified to reduce typical running time from O(N^2) to O(N)

    GridLayout converts its requirements into a system of linear constraints of the form:

    x[i] - x[j] < a[k]
    

    Where the x[i] are variables and the a[k] are constants.

    For example, if the variables were instead labeled x, y, z we might have:

    x - y < 17
    y - z < 23
    z - x < 42
    

    This is a special case of the Linear Programming problem that is, in turn, equivalent to the single-source shortest paths problem on a digraph, for which the O(n^2) Bellman-Ford algorithm the most commonly used general solution.

    It has a solve method that is using linear programming to guarantee the consistency of the constraints it has to satisfy, given its configuration. You can probably improve your layout performance if you figure out which configuration is associated with the constraint x5 - x4 < 221 and remove it. Then the solver won't have to solve that it can't be satisfied and remove it itself.

    0 讨论(0)
  • 2021-01-01 11:37

    For me I was creating a custom view using GridLayout.

    The problem was that I thought I could set the grid's column count inside my xml.

    I had layout XML that looked like this:

    <merge 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"
    
            app:alignmentMode="alignMargins"
            app:columnCount="9"
            app:columnOrderPreserved="true"
            tools:ignore="HardcodedText"
            app:orientation="horizontal"
            tools:parentTag="androidx.gridlayout.widget.GridLayout"
            app:rowOrderPreserved="true">
    
            ...
    
    </merge>
    

    Unfortunately it does not work this way for custom layouts. I had to specify all those attributes in namespace app, in my custom view like this:

    class SimpleCalculatorView(context: Context, attrs: AttributeSet?): GridLayout(context, attrs) {
    
      init {
        ...
    
        View.inflate(context, R.layout.view_simple_calculator, this)
        columnCount = 9
        columnOrderPreserved = true
        rowOrderPreserved = true
        orientation = HORIZONTAL
    }
    

    After doing this, I no longer got the error.

    EDIT

    I spoke too soon. The error came back again and this time it started happening whenever I animate the custom layout in a motionlayout.

    0 讨论(0)
  • 2021-01-01 11:40

    I had the same issue and I found that I missed to add XML namespace. Corrected it in this way:

    <android.support.v7.widget.GridLayout 
         xmlns:grid="http://schemas.android.com/apk/res-auto"
         xmlns:android="http://schemas.android.com/apk/res/android">
    ...
    </android.support.v7.widget.GridLayout>
    

    Then changed prefix of attributes used by compatibility GridLayout with XML namespace too:

    <ImageButton android:id="@+id/btnSentence"
        grid:layout_row="0"
        grid:layout_column="0"
        ...
    />
    

    and it helped... Hope it helps you too.

    0 讨论(0)
  • 2021-01-01 11:49

    I made the issue go away by using wrap_content for the GridLayout's width instead of match_parent, I guess it is one less constraint for it to worry about.

    0 讨论(0)
提交回复
热议问题