如果我独自一人在行中,GridLayout 添加视图以消耗所有可用宽度

问题描述

在我的 xml 中有一个 GridLayout 并通过动态添加视图我有这个代码

activity.xml

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/addBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <GridLayout
        android:id="@+id/gridLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:columnCount="2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/addBtn" />

</androidx.constraintlayout.widget.ConstraintLayout>

项目布局xml

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

和活动代码

private var counter = 0
addBtn.setonClickListener {
            counter++
            addView()
        }

fun addView() {

        val item = this.layoutInflater
            .inflate(R.layout.grid_item,gridLayout,false)
        val params = GridLayout.LayoutParams(
            GridLayout.spec(
                GridLayout.UNDEFINED,GridLayout.FILL,1f
            ),GridLayout.spec(
                GridLayout.UNDEFINED,1f
            )
        ).also {
            it.height = 0
            it.width = 0
        }

        gridLayout.columnCount = if (counter == 2) 1 else 2
        item.layoutParams = params

        val textView = (item as ViewGroup).getChildAt(0) as TextView
        textView.text = "Text with counter $counter"

        gridLayout.addView(item)

    }

我的问题是,例如,我想要位于第二行的第三项(带有计数器 3 的 TextView),我希望它扩展到 GridLayout 的所有宽度。现在它只需要半宽(因为我有 2 列)。 我该怎么做?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...