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

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

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>();

a.add("Acuminatus" + "\n\nType of leaf");
.....
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

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

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

&quot;0 Acuminatus&quot;

Or

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

But its still not working

More the code

Private List&lt;String&gt; populateA() {
List&lt;String&gt; a=new ArrayList&lt;String&gt;();

a.add(&quot;Acuminatus&quot;+&quot;\n \n Type of leaf&quot;);
..... 
Return a;
}

答案1

得分: 1

使用以下方法来显示:

public static Spanned fromHtml(String html) {
    Spanned result;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
        result = Html.fromHtml(html, FROM_HTML_MODE_LEGACY);
    } else {
        result = Html.fromHtml(html);
    }
    return result;
}

调用方式如下:

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

Use the below method where you are going to display:

public static Spanned fromHtml(String html) {
        Spanned result;
        if (android.os.Build.VERSION.SDK_INT &gt;= android.os.Build.VERSION_CODES.N) {
            result = Html.fromHtml(html, FROM_HTML_MODE_LEGACY);
        } else {
            result = Html.fromHtml(html);
        }
        return result;
    }

Calling Like:

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

答案2

得分: 0

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

SpannableString output = new SpannableString("Acuminatus" + " 叶的类型");
output.setSpan(new StyleSpan(Typeface.ITALIC), 0, 11, 0);
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

SpannableString output = new SpannableString(&quot;Acuminatus&quot; + &quot; Type of leaf&quot;);
output.setSpan(new StyleSpan(Typeface.ITALIC), 0, 11, 0);
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:

确定