【问题标题】:Resource String Color not working programmatically in Android资源字符串颜色在 Android 中无法以编程方式工作
【发布时间】:2021-09-16 12:10:51
【问题描述】:

string.xml 这是我的字符串

<string name="bullet_text"><font color="#38d98a">●</font>\t\tCucumber Detox Water (1 glass)\n<font color="#38d98a">●</font>\t\tSkimmed Milk (1 glass)\n<font color="#38d98a">●</font>\t\tPeas Poha (1.5 katori)</string>

当这个字符串在 xml 中使用时它工作得很好,但是当以编程方式使用这个字符串时它就不起作用了

xml 代码

<TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/bullet_text"
        android:textSize="20sp"
        android:layout_margin="10dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

完美运行

Kotlin 代码

 val textView:TextView = findViewById(R.id.textView)
 textView.text = getString(R.string.bullet_text)

工作不完美

【问题讨论】:

  • 试试Like This。
  • 它可以工作,但字符串中的 \n 不工作
  • 现在可以了,我将 \n 替换为

标签: android string android-studio textview


【解决方案1】:

现在可以使用答案在这里感谢@ADM 和@Rasheed 提供评论帮助

strings.xml

<string name="bullet_text"><![CDATA[<font color="#38d98a">●</font>\t\tCucumber Detox Water (1 glass)<br/><font color="#38d98a">●</font>\t\tSkimmed Milk (1 glass)<br/><font color="#38d98a">●</font>\t\tPeas Poha (1.5 katori)]]></string>

Kotlin 代码

val textView: TextView = findViewById(R.id.textView)
        textView.text = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Html.fromHtml(getString(R.string.hello_worldRed), Html.FROM_HTML_MODE_COMPACT)
        } else {
            Html.fromHtml(getString(R.string.hello_worldRed))
        }

输出

【讨论】:

    【解决方案2】:

    您可以使用 SpannableString 在字符串之前设置项目符号:

    val string = SpannableString("Text with\nBullet point")
            string.setSpan(
                BulletSpan(40, Color.GREEN, 20),
                10,
                22,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
            )
            binding.textView.text = string
    

    这是结果

    另见->BulletSpan

    更新

    if (Build.VERSION.SDK_INT >= 28) {
    
        val string = SpannableString("Text with\nBullet point")
        string.setSpan(
            BulletSpan(40, Color.GREEN, 20),
            10,
            22,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        binding.textView.text = string
    }else {
            // your own code
        }
    

    【讨论】:

    • 它在 API 级别 28 以下不起作用
    • @Yaqoob Bhatti 现在好了
    【解决方案3】:
    You can use custom style instead of string
    <style name="CustomFontStyle">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:capitalize">characters</item>
        <item name="android:typeface">monospace</item>
        <item name="android:textSize">12pt</item>
        <item name="fontFamily">your font</item>
        <item name="android:textColor">#38d98a</item>/>
    </style>
    

    【讨论】:

    • 但在我的场景中我需要字符串
    • 如果你想将字符串分成两部分,请使用
    • 注册
      享受新的购物生活 stack.com!
    猜你喜欢
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-05
    相关资源
    最近更新 更多