【问题标题】:How to create a coloarable TextView on Android (Like IDE)如何在 Android 上创建可着色的 TextView(如 IDE)
【发布时间】:2014-02-08 13:27:57
【问题描述】:

我想创建一个包含编程语言代码的 TextView。如下图

我认为我应该使用 html 来赋予颜色,也会使用字符串函数,例如 indexof、substring .. 首先,会找到“for”,如果我找到它,我会将其替换为

  <h3 style="color:blue;margin-left:20px;">for</h3> 

但我不确定它是否是最好的方法还是有用的......你有什么想法在 android 上做吗?

PS:我不是在做编译器,只是想展示一段代码

PS2:我不是要求 webview 或 textview.. 只是想了解除了我的 html 案例之外我该怎么做。没关系 webview 或 textview 或 editview

【问题讨论】:

  • 您可以使用 WebView 代替 TextView。并使用您存储在 assets 文件夹中的一些 html 文件 填充其内容。确实很容易。
  • @ArtooDetoo,感谢您的回答。是的,我知道我应该将 webview 用于 html 文件,但我想问一下如何用其他方式来处理它?意味着可能不使用html代码?
  • 您的最佳选择是 WebView。但是,如果您真的想使用 Textview... 请参阅 this
  • @ArtooDetoo,我看到了thanx,但我的问题不在于如何在 texview 上显示 html 之类的。我正在寻找不同的方式?这与 html 无关。这只是我的一个想法。也许在图片中,他们使用了另一种方式?我想学。。
  • 这是另一个问题!您会使用正则表达式来进行语法着色...谷歌搜索并找到不计其数的示例。

标签: android textview color-scheme


【解决方案1】:

试试这个

public static Spannable setFourDifferentFontStyle(final Context context, String original, 
                                                 String firstWord, String color1, float size, 
                                                 String secondWord, String color2, float size2, 
                                                 String thirdWord, String color3, float size3, 
                                                 String fourthWord, String color4, float size4)

 {
  // Create a new spannable with the two strings
  Spannable spannable = new SpannableString(firstWord + secondWord + thirdWord + fourthWord);

  spannable.setSpan(new RelativeSizeSpan(size), 0, firstWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  spannable.setSpan(new ForegroundColorSpan(Color.parseColor(color1)), 0, firstWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

  spannable.setSpan(new RelativeSizeSpan(size2), firstWord.length(), firstWord.length() + secondWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  spannable.setSpan(new ForegroundColorSpan(Color.parseColor(color2)), firstWord.length(),firstWord.length() + secondWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

  spannable.setSpan(new RelativeSizeSpan(size3), firstWord.length() + secondWord.length(), firstWord.length() + secondWord.length() + thirdWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  spannable.setSpan(new ForegroundColorSpan(Color.parseColor(color3)), firstWord.length() + secondWord.length(),firstWord.length() + secondWord.length() + thirdWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

  spannable.setSpan(new RelativeSizeSpan(size4), firstWord.length() + secondWord.length() + thirdWord.length(), original.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  spannable.setSpan(new ForegroundColorSpan(Color.parseColor(color4)), firstWord.length() + secondWord.length() + thirdWord.length(), original.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}

【讨论】:

  • 这样你就不能有动态字符串,除非你想在每次写之前搜索文本文本中的关键术语。
【解决方案2】:

感谢大家的关注,我已经完全找到了我想要的解决方案。 我使用了codemirror 库,并找到了一个适用于 android 的示例。

你可以从code google下载项目

【讨论】:

  • 感谢分享ertan2002
猜你喜欢
  • 2017-04-27
  • 1970-01-01
  • 2011-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-21
  • 1970-01-01
相关资源
最近更新 更多