android - 有没有办法以编程方式使用约束布局助手小部件流?

标签 android android-constraintlayout

我想使用约束布局 androidx.constraintlayout.helper.widget.Flow 小部件,但是当我搜索时,我找不到任何关于以编程方式使用流小部件的示例。如何以编程方式设置 constraint_referenced_ids?

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<androidx.constraintlayout.helper.widget.Flow
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:constraint_referenced_ids="text1,text2"
    app:flow_horizontalBias="0"
    app:flow_horizontalGap="10dp"
    app:flow_horizontalStyle="packed"
    app:flow_verticalBias="0"
    app:flow_wrapMode="chain"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"

    />

最佳答案

这对我有用:

我的定制PerkFlow类(class):

class PerkFlow(context: Context, attrs: AttributeSet?) : Flow(context, attrs) {

fun setup(
    parentView: ViewGroup,
    perks: List<String>
) {
    val referencedIds = IntArray(perks.size)
    for (i in perks.indices) {
        val textView = createTextView(context)
        textView.text = perks[i]
        textView.id = View.generateViewId()

        parentView.addView(textView)
        referencedIds[i] = textView.id
    }
    this.referencedIds = referencedIds
}

private fun createTextView(context: Context): TextView {
    val textView = TextView(context)
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12F)
    textView.setTextColor(context.resources.getColor(R.color.gmm_white))
    return textView
}
}

我的 xml :

  <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

..... (a lot of other code)

  <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/vendor_details_perks_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/vendor_details_favourite">

            <com.perkapp.mobile.views.PerkFlow
                android:id="@+id/vendor_details_perks"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                app:flow_horizontalBias="0"
                app:flow_horizontalGap="5dp"
                app:flow_horizontalStyle="packed"
                app:flow_verticalBias="0"
                app:flow_verticalGap="2dp"
                app:flow_wrapMode="chain"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>

 ..... (a lot of other code)

 </androidx.constraintlayout.widget.ConstraintLayout>

Fragment 中 PerkFlow 的设置:

  binding.vendorDetailsPerks.setup(
            binding.vendorDetailsPerksContainer,
            listOf("apple, banana, blackberries, blueberries, cherries, grapes, lemon, orange, peaches, pear, pineapple, plums, raspberries, strawberries, watermelon ")
        )

希望这可以帮助!

还有一件事:如果你再次调用它(在 RecyclerView 项目中,或者有新数据到达),不要忘记清除父 View ,否则元素将被复制。如果需要,我也可以发送该代码。

关于android - 有没有办法以编程方式使用约束布局助手小部件流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61487630/

相关文章:

android - 如何获取 Android 崩溃日志?

android - 无法通过 Ubuntu 终端构建 Android Studio 项目

安卓火力地堡 : how to change objects with specific field value?

Android:BottomSheetDialog 未使用 ConstraintLayout 完全展开

android - 如何在没有 gradle 的情况下使用 androidx 库

android - Android Studio 中的 NDK 集成错误

android - 为什么 getEnabledProtocols() 返回 TLS v1 而不是 TLSv1.2?

android - 使用 ConstraintLayout 作为 DialogFragment 的根布局时的奇怪行为

Android约束布局包括另一个布局

android - 如何在嵌套滚动、ConstraintLayout 中设置ListView 滚动?