如何在 TextView 中显示格式化文本? [英] How can I display formatted text in a TextView?

查看:28
本文介绍了如何在 TextView 中显示格式化文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 TextView 中显示格式化文本?

How can I display formatted text in a TextView?

简单地将格式化文本复制并粘贴到字符串资源文件中也不起作用.

Simply copying and pasting formatted text to string resource file doesn't work either.

<string name="some_text">Some Formatted text copied from browser</string>

Java:

TextView textView = (TextView) findViewById(R.id.some);
textView.setText(getString(R.string.some_text));

但它显示为普通文本.要求是显示彩色或粗体文本等.

But it displays like normal text. The requirement is to display coloured or bold texts and more.

推荐答案

如果您想设置文本样式,您有几个选择.

If you want to style your text you have a few options.

最简单的方法是使用 HTML 标签.不过,您必须使用 CDATA 包装它.

The easiest would be to use HTML tags. You do have to wrap it with CDATA though.

例如,您可以创建您的字符串:

For example, you can create your String:

<string name="some_text"><![CDATA[Create stickers for <b>Gboard</b>...]]></string>

然后显示它,你可以这样做.

And then to display it, you can do this.

Spanned sp = Html.fromHtml( getString(R.string.some_text));
tv.setText(sp);

<小时>

现在,更可靠的方法是实际使用 Android 的样式.


Now, the more robust way would be to actually use Android's styles.

您需要创建多个 TextView,每个都有自己的样式,这样您就可以对文本的确切外观进行大量控制.

You would need to create multiple TextViews, each with their own Style, and that would allow you a lot of control over the exact look of the text.

例如,这是一个您可以执行类似操作的布局.

For example, here is a layout you could do something similar to.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <!-- Creating a Title for your layout. -->
    <TextView
        style="@style/TextAppearance.AppCompat.Title" <!-- Title Style -->
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Create stickers for Gboard on Google Play"
         />
    <!-- Creating a subheader for your layout. -->
    <TextView
        style="@style/TextAppearance.AppCompat.Caption" <!-- Caption Style -->
        android:id="@+id/subheader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="01 September 2017"
         />

</LinearLayout>

对于您拥有的每种不同格式,您需要创建一个新的 TextView,并应用正确的样式.

For each different format you have, you need to create a new TextView, and apply the correct style.

我建议坚持使用 @style/TextAppearance.AppCompat... 下的任何内容,因为这将为您提供文本大小/重量的一个很好的起点.

I would suggest sticking with anything under @style/TextAppearance.AppCompat..., as that will give you a very good starting point with size/weight of your text.

您可以使用TitleCaptionBody2Body1 等内容.

You can use stuff like Title, Caption, Body2, Body1, and more.

然后,当您对样式有更多经验时,您可以开始创建自己的样式.

And then down the road, when you have more experience with Styles, you can start creating your own.

例如,假设我希望我的所有标题都是 RED.您首先在 styles.xml

For example, say I want all my Titles to be RED. You first create a Style within styles.xml

<style name="myTitleStyle" parent="TextAppearance.AppCompat.Title">
    <item name="android:textColor">@color/red</item>
</style>

然后就可以像以前的样式一样使用了.

And then you can use it as the previous styles.

<!-- Creating a Title for your layout. -->
        <TextView
            style="@style/myTitleStyle" <!-- myTitleStyle Style -->
            android:id="@+id/header"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Create stickers for Gboard on Google Play"
             />

这篇关于如何在 TextView 中显示格式化文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆