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

Android Studio类中如何用字符串替代硬编码并实现文本格式化

解决方案

字符串资源引用方法

你之前引用字符串资源失效的核心原因:R.string.xxx 返回值是整数类型的资源ID,并非实际字符串内容,直接传入需要String类型参数的构造方法会导致显示异常。在Fragment中必须通过getString()方法解析资源ID,拿到实际字符串后再赋值。
操作步骤:

  • 先在res/values/strings.xml中定义所有需要的文本:
<resources>
    <string name="parent_title_mac">MAC</string>
    <string name="child_sample">Sample Text here</string>
    <string name="parent_title_stack">Stack</string>
    <string name="child_joy">Joy</string>
    <string name="child_king">King</string>
    <string name="child_wink">Wink</string>
    <string name="parent_title_teso">TESO</string>
    <string name="child_sting">Sting</string>
    <string name="child_run">Run</string>
    <string name="child_fridge">Fridge</string>
</resources>
  • 代码中通过requireContext().getString(资源ID)获取字符串后,再传入Parent、Child的构造方法即可。

文本格式化(字号、粗体)实现

优先推荐在布局文件中配置固定样式,动态样式再通过代码实现:

  • 固定样式:直接在RecyclerView对应的父项、子项item布局里,给显示标题的TextView配置属性即可,维护成本最低:
    • 粗体:添加属性android:textStyle="bold"
    • 字号:添加属性android:textSize="16sp",字号单位必须用sp,适配系统字体缩放设置
  • 动态差异化样式:在ItemsAdapter的onBindViewHolder生命周期中,针对绑定的TextView动态设置属性:
// 设置粗体
holder.titleText.paint.isFakeBoldText = true
// 设置字号,第二个参数为字号数值,单位sp
holder.titleText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f)
// 如果需要给同一段文本的不同部分设置不同样式,使用SpannableString实现即可

修正后可运行的Fragment代码

(已修复原代码的语法错误:参数名attachtoroot拼写错误、部分字符串缺失闭合引号)

import android.os.Bundle
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import java.util.*

class ItemsFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        val rootView = inflater.inflate(R.layout.fragment_list, container, attachToRoot = false)
        val context = requireContext()

        val parent1 = Parent(id = 0, title = context.getString(R.string.parent_title_mac))
        val childItems1 = ArrayList<Child>()
        childItems1.add(Child(parent1, id = 0, title = context.getString(R.string.child_sample)))
        parent1.childItems.clear()
        parent1.childItems.addAll(childItems1)

        val parent2 = Parent(id = 1, title = context.getString(R.string.parent_title_stack))
        val childItems2 = ArrayList<Child>()
        childItems2.add(Child(parent2, id = 9, title = context.getString(R.string.child_joy)))
        childItems2.add(Child(parent2, id = 10, title = context.getString(R.string.child_king)))
        childItems2.add(Child(parent2, id = 11, title = context.getString(R.string.child_wink)))
        parent2.childItems.clear()
        parent2.childItems.addAll(childItems2)

        val parent3 = Parent(id = 2, title = context.getString(R.string.parent_title_teso))
        val childItems3 = ArrayList<Child>()
        childItems3.add(Child(parent3, id = 12, title = context.getString(R.string.child_sting)))
        childItems3.add(Child(parent3, id = 13, title = context.getString(R.string.child_run)))
        childItems3.add(Child(parent3, id = 14, title = context.getString(R.string.child_fridge)))
        parent3.childItems.clear()
        parent3.childItems.addAll(childItems3)

        val itemList = ArrayList<Item>()
        itemList.add(parent1)
        itemList.add(parent2)
        itemList.add(parent3)

        val list = rootView.findViewById<RecyclerView>(R.id.list)
        list.adapter = ItemsAdapter(itemList)

        return rootView
    }

    companion object {
        fun getInstance() = ItemsFragment()
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.02 07:57:27