微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如果我独自一人在行中,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 举报,一经查实,本站将立刻删除。