我们如何动态地添加Views androidx.constraintlayout.helper.widget.Flow并添加参考ID [英] How can we add Views dynamically androidx.constraintlayout.helper.widget.Flow and add reference Ids

查看:813
本文介绍了我们如何动态地添加Views androidx.constraintlayout.helper.widget.Flow并添加参考ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何在androidx.constraintlayout.helper.widget.Flow中动态添加视图并动态添加引用ID.

How can we add Views dynamically in androidx.constraintlayout.helper.widget.Flow and add reference Ids dynamically.

推荐答案

这是Kotlin的实现

Here is Kotlin Implementation

for (i in 0..4) {
    val customView = CustomComponent (this)
    customView.id = generateViewId()
    constraintLayout.addView(customView,i)
    flow.addView(customView)
}

以下是我的XML

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/constraintLayout"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    >

    <androidx.constraintlayout.helper.widget.Flow
        android:id="@+id/flow"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:flow_wrapMode="chain"
        app:flow_horizontalGap="2dp"
        app:flow_verticalGap="2dp"
        app:flow_verticalStyle = "spread_inside"
        app:flow_horizontalStyle="packed"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

以下是我的自定义视图

class CustomComponent @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyle: Int = 0,
defStyleRes: Int = 0):LinearLayout(context, attrs, defStyle, defStyleRes) {
init {
    LayoutInflater.from(context)
        .inflate(R.layout.custom_view, this, true)
}}

这篇关于我们如何动态地添加Views androidx.constraintlayout.helper.widget.Flow并添加参考ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆