在Android Studio中如何使句子中的单词倾斜?

huangapple go评论111阅读模式
英文:

How to italic a word in sentence on Android studio?

问题

I'm new at developing an Android app. I wanted to make some of the words into italic, but it's not working properly.
Can you tell me is it possible to make a word in sentence into italic? If it's possible tell me how to make it italic?

我是新手开发Android应用程序。我想将一些单词变成斜体,但它不起作用。
你能告诉我在句子中将单词变成斜体是否可能吗?如果可能,请告诉我如何使它变成斜体?

I made a glossary with a ListView, the sentence I made it in an ArrayList like this

我用ListView制作了一个词汇表,我将句子制作成了一个ArrayList,像这样

a.add("Acuminatus" + "Type of leaf");

a.add("Acuminatus" + "叶子的类型");

How to italicize the word "Acuminatus"? I have tried to add it like this

如何使单词“Acuminatus”变成斜体?我尝试添加如下

"<i>Acuminatus</i>"

但仍然不起作用

More of the code

更多的代码

private List<String> populateA() {
List<String> a = new ArrayList<String>();

  1. a.add("Acuminatus" + "\n\nType of leaf");
  2. .....
  3. return a;

}

英文:

I'm new at developing an Android app. I wanted to make some of the words into italic, but it's not working properly.
Can you tell me is it possible to make a word in sentence into italic? If it's possible tell me how to make it italic?

I made a glosarium with listview, the sentence I made it on array list like this

  1. a.add(&quot;Acuminatus&quot;+&quot;Type of leaf&quot;);

How to italic the word "Acuminatus" I have tried to add like

  1. &quot;0 Acuminatus&quot;

Or

  1. &quot;&lt;i&gt;Acumunatus&lt;/i&gt;&quot;

But its still not working

More the code

  1. Private List&lt;String&gt; populateA() {
  2. List&lt;String&gt; a=new ArrayList&lt;String&gt;();
  3. a.add(&quot;Acuminatus&quot;+&quot;\n \n Type of leaf&quot;);
  4. .....
  5. Return a;
  6. }

答案1

得分: 1

使用以下方法来显示:

  1. public static Spanned fromHtml(String html) {
  2. Spanned result;
  3. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
  4. result = Html.fromHtml(html, FROM_HTML_MODE_LEGACY);
  5. } else {
  6. result = Html.fromHtml(html);
  7. }
  8. return result;
  9. }

调用方式如下:

  1. textview.setText(fromHtml("<i>Acumunatus</i> Type of leaf"))
英文:

Use the below method where you are going to display:

  1. public static Spanned fromHtml(String html) {
  2. Spanned result;
  3. if (android.os.Build.VERSION.SDK_INT &gt;= android.os.Build.VERSION_CODES.N) {
  4. result = Html.fromHtml(html, FROM_HTML_MODE_LEGACY);
  5. } else {
  6. result = Html.fromHtml(html);
  7. }
  8. return result;
  9. }

Calling Like:

  1. textview.setText(fromHtml(&quot;&lt;i&gt;Acumunatus&lt;/i&gt; Type of leaf&quot;))

答案2

得分: 0

你可以使用SpannableStringStyleSpan类型的Span。调用setSpan()方法,该方法接受要样式化的文本的索引。

  1. SpannableString output = new SpannableString("Acuminatus" + " 叶的类型");
  2. output.setSpan(new StyleSpan(Typeface.ITALIC), 0, 11, 0);
  3. textview.setText(output);
英文:

You can use SpannableString with a Span of type StyleSpan .. Call setSpan() that takes the indices of the text that you want to style

  1. SpannableString output = new SpannableString(&quot;Acuminatus&quot; + &quot; Type of leaf&quot;);
  2. output.setSpan(new StyleSpan(Typeface.ITALIC), 0, 11, 0);
  3. textview.setText(output);

huangapple
  • 本文由 发表于 2020年8月6日 11:09:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63276278.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定