You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现字符串数组逐词拆字符并在独立TextView中显示?

问题解决:遍历字符串数组拆分字符并生成TextView

你的代码只处理了数组里第一个单词(i被固定赋值为0),而且生成的TextView没有设置布局参数,导致所有字符TextView重叠在同一位置,看起来就像只创建了一个。

解决步骤

  • 遍历整个palabras数组,处理每一个单词
  • 给动态创建的TextView设置布局规则,避免重叠

方案一:使用ConstraintLayout实现水平排列

val palabras = arrayOf("Escalera", "Fruta", "Calor", "Automóvil", "Barco", "Sandía", "Aviación", "Fábrica", "Utensilio", "Can", "Perro", "Malabarista", "Hotel", "Juego", "Continente", "Población", "Pirámide", "Átomo", "Molécula", "Agua", "Hidrógeno", "Chimenea", "Polvillo")

var previousTextView: TextView? = null

// 遍历所有单词
for (word in palabras) {
    // 拆分单词为字符并逐个创建TextView
    for (char in word.toCharArray()) {
        val textView = TextView(this).apply {
            text = char.toString()
            id = View.generateViewId() // 生成唯一ID用于约束设置
            textSize = 18f
            setPadding(8, 8, 8, 8)
        }

        constraintLayout.addView(textView)

        // 设置ConstraintLayout约束,避免重叠
        val layoutParams = textView.layoutParams as ConstraintLayout.LayoutParams
        layoutParams.apply {
            topToTop = ConstraintLayout.LayoutParams.PARENT_ID
            leftToRight = previousTextView?.id ?: ConstraintLayout.LayoutParams.PARENT_ID
        }
        textView.layoutParams = layoutParams

        previousTextView = textView
    }

    // 添加空格分隔不同单词的字符
    val spaceView = TextView(this).apply {
        text = " "
        id = View.generateViewId()
        textSize = 18f
    }
    constraintLayout.addView(spaceView)
    val spaceLayoutParams = spaceView.layoutParams as ConstraintLayout.LayoutParams
    spaceLayoutParams.apply {
        topToTop = ConstraintLayout.LayoutParams.PARENT_ID
        leftToRight = previousTextView!!.id
    }
    spaceView.layoutParams = spaceLayoutParams
    previousTextView = spaceView
}

方案二:使用LinearLayout实现自动排列(更简单)

先在XML布局中添加一个LinearLayout:

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="8dp"/>

然后在代码中添加TextView:

val palabras = arrayOf("Escalera", "Fruta", "Calor", "Automóvil", "Barco", "Sandía", "Aviación", "Fábrica", "Utensilio", "Can", "Perro", "Malabarista", "Hotel", "Juego", "Continente", "Población", "Pirámide", "Átomo", "Molécula", "Agua", "Hidrógeno", "Chimenea", "Polvillo")

val linearLayout = findViewById<LinearLayout>(R.id.linearLayout)

for (word in palabras) {
    // 拆分单词为字符并添加TextView
    for (char in word.toCharArray()) {
        val textView = TextView(this).apply {
            text = char.toString()
            textSize = 18f
            setPadding(8, 8, 8, 8)
        }
        linearLayout.addView(textView)
    }
    // 添加空格分隔单词
    val spaceView = TextView(this).apply {
        text = " "
        textSize = 18f
    }
    linearLayout.addView(spaceView)
}

内容的提问来源于stack exchange,提问作者Mauro Stancato

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 07:40:39