Android的GridLayout“约束不一致”警告是关于什么的?

时间:2019-08-28 09:28:28

标签: java android kotlin compiler-warnings android-gridlayout

我为main.xml设置了20列32行的GridLayout。我在MainActivity的onCreate()中以编程方式用一个名为GridCell的自定义视图填充GridLayout,以便以后可以通过代码在每个单元格中设置颜色。 下面提供的代码工作正常,但是每当我启动应用程序时,运行控制台就会被这些警告淹没:

  

D / android.widget.GridLayout:垂直约束:y32-y0> = 1784,y32-y31 <= 55,y31-y30 <= 55,y30-y29 <= 55,y29-y28 <= 55,y28 -y27 <= 55,y27-y26 <= 55,y26-y25 <= 55,y25-y24 <= 55,y24-y23 <= 55,y23-y22 <= 55,y22-y21 <= 55,y21- y20 <= 55,y20-y19 <= 55,y19-y18 <= 55,y18-y17 <= 55,y17-y16 <= 55,y16-y15 <= 55,y15-y14 <= 55,y14-y13 <= 55,y13-y12 <= 55,y12-y11 <= 55,y11-y10 <= 55,y10-y9 <= 55,y9-y8 <= 55,y8-y7 <= 55,y7-y6 < = 55,y6-y5 <= 55,y5-y4 <= 55,y4-y3 <= 55,y3-y2 <= 55,y2-y1 <= 55,y1-y0 <= 55是不一致的;永久删除:y32-y31 <= 55。

是否需要担心应用程序的长期性能?

我已经在网上搜索并找到了这两个主题,但是没有一个为我提供解决方案:

GridLayout - Vertical/Horizontal constraints are inconsistent

GridLayout spitting out "inconsistent constraint" debug-level logs

第二个主题提到我必须找出导致该问题的约束条件,但是对它的评论表明,要找出实际上是哪个约束条件似乎非常困难。

我试图从androidx.gridLayout移至android.widget.gridLayout,但随后代码崩溃,因为widget.gridLayout不支持gridLayout.coloumnCount和gridLayout.rowCount,我的代码依赖于此。

我的main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frameLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/settingsButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="75px"
        android:layout_marginTop="50px"
        android:onClick="onClick"
        android:text="Settings" />

    <ImageView
        android:id="@+id/airplane"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="65dp"
        android:paddingRight="65dp"
        android:src="@drawable/image" />

    <GridLayout
        android:id="@+id/gridLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:columnCount="20"
        android:rowCount="32" >
    </GridLayout>
</FrameLayout>

我的MainActivity:

class MainActivity : RosActivity("MainActivity","MainActivity") {

    private val topicName = "grid"
    private lateinit var randomGridGenerator : RandomGridGenerator
    private lateinit var gridReceiver : GridReceiver
    private lateinit var gridCells : Array<GridCell>

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

        setupGrid()

        randomGridGenerator = RandomGridGenerator(gridLayout, topicName, applicationContext)
        gridReceiver = GridReceiver(gridLayout, topicName, applicationContext)
    }

    override fun init(nodeMainExecutor: NodeMainExecutor) {
        // At this point, the user has already been prompted to either enter the URI
        // of a master to use or to start a master locally.

        val nodeConfiguration = NodeConfiguration.newPublic(rosHostname)
        nodeConfiguration.masterUri = masterUri

        //nodeMainExecutor.execute(randomGridGenerator, nodeConfiguration)
        nodeMainExecutor.execute(gridReceiver, nodeConfiguration)
    }

    fun onClick(view: View){
        startMasterChooserActivity()
    }

    private fun setupGrid(){
        val numOfCols = gridLayout.columnCount
        val numOfRows = gridLayout.rowCount
        gridCells = Array(numOfCols*numOfRows){GridCell(applicationContext)}
        for (yPos in 0 until numOfRows) {
            for (xPos in 0 until numOfCols){
                val gridCell = GridCell(this, xPos, yPos)
                gridCells[yPos * numOfCols + xPos] = gridCell
                gridLayout.addView(gridCell)
            }
        }

        gridLayout.viewTreeObserver.addOnGlobalLayoutListener {
            val margin = 1
            val pWidth = gridLayout.width
            val pHeight = gridLayout.height
            val w = pWidth / numOfCols
            val h = pHeight / numOfRows

            for (yPos in 0 until numOfRows) {
                for (xPos in 0 until numOfCols) {
                    val params = gridCells[yPos * numOfCols + xPos].layoutParams as GridLayout.LayoutParams
                    params.width = w - 2 * margin
                    params.height = h - 2 * margin
                    params.setMargins(margin, margin, margin, margin)
                    gridCells[yPos * numOfCols + xPos].layoutParams = params
                }
            }
        }
    }

    private fun startMasterChooserActivity() {
        val intent = Intent(this, MasterChooser::class.java)
        intent.putExtra("comingFromRosActivity", 1)
        startActivity(intent)
    }
}

我的GridCell视图:

class GridCell : View {

    private var touchOn: Boolean = false
    var idX = 0
        internal set    //default
    var idY = 0
        internal set    //default

    constructor(context: Context, x: Int, y: Int) : super(context) {
        idX = x
        idY = y
        init()
    }

    constructor(context: Context) : super(context) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        init()
    }

    private fun init() {
        touchOn = false
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        setMeasuredDimension(
            MeasureSpec.getSize(widthMeasureSpec),
            MeasureSpec.getSize(heightMeasureSpec)
        )
    }
}

0 个答案:

没有答案