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
相关产品推荐
相关产品推荐

