首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我们如何动态地添加视图androidx.constraintlayout.helper.widget.Flow和添加引用Ids

我们如何动态地添加视图androidx.constraintlayout.helper.widget.Flow和添加引用Ids
EN

Stack Overflow用户
提问于 2019-11-28 16:24:56
回答 3查看 3.2K关注 0票数 12

如何在androidx.constraintlayout.helper.widget.Flow中动态添加视图和动态添加引用Ids。

EN

回答 3

Stack Overflow用户

发布于 2019-12-03 22:48:05

您应该首先将您的视图(设置了id )添加到父ConstraintLayout。然后你可以用Flow.addView()将它的引用id添加到你的Flow中。例如:

代码语言:javascript
运行
复制
val view = LayoutInflater.from(context).inflate(R.layout.item, this, false)
view.layoutParams = ConstraintLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT)
view.id = View.generateViewId()
constraintLayout.addView(view)
flow.addView(view)

使用此xml作为ViewGroup:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/constraintLayout"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

  <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:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      />
</androidx.constraintlayout.widget.ConstraintLayout>
票数 8
EN

Stack Overflow用户

发布于 2020-02-22 03:12:11

下面是Kotlin实现

代码语言:javascript
运行
复制
for (i in 0..4) {
    val customView = CustomComponent (this)
    customView.id = generateViewId()
    constraintLayout.addView(customView,i)
    flow.addView(customView)
}

以下是我的XML

代码语言:javascript
运行
复制
<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>

以下是我的自定义视图

代码语言:javascript
运行
复制
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)
}}
票数 4
EN

Stack Overflow用户

发布于 2020-02-17 16:45:28

代码语言:javascript
运行
复制
view1.setId(View.generateViewId());
view2.setId(View.generateViewId());
view3.setId(View.generateViewId());
flow.setReferencedIds(new int[]{view1.getId(), view2.getId(), view3.getId()});
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59084494

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档