【问题标题】:How can I make the text in TextView be two different sizes?如何使 TextView 中的文本具有两种不同的大小?
【发布时间】:2011-12-12 22:41:10
【问题描述】:

我想在 TextView 中写一行,其中第二个单词是第一个单词大小的四分之一。在我的 string.xml 文件中,我使用了大大小小的文件,但它并没有达到我想要的程度。我还尝试更改 main.xml 中的文本大小,但它只允许您在 TextEdit 中执行一次。有什么想法吗?

【问题讨论】:

    标签: android xml textedit


    【解决方案1】:

    如果您需要从 XML 为 TextView 中的文本指定不同的字体大小,您可以分配如下字符串:

    <string name="hello"><big><big>Hello Big Text!</big></big>\n<small>Hello small text</small></string>
    

    并在 TextView 中使用此字符串。请注意,换行符是 '\n' 而不是 &lt;br/&gt;

    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:text="@string/hello" />
    

    【讨论】:

      【解决方案2】:

      看看这个问题:Is it possible to change the text color in a string to multiple colors in Java?

      但你应该使用TextAppearanceSpanAbsoluteSizeSpan 而不是ForegroundColorSpan

      【讨论】:

      • 在java中是可以的。我想知道我是否可以在 xml 中做到这一点
      • 不,你不能。您需要以一种或另一种方式使用 java。
      【解决方案3】:

      尝试使用HTML 类的fromHTML 方法。

      这样你就可以使用基本的 HTML &lt;font&gt; 标签来格式化字符串

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-16
        • 2020-10-30
        • 2018-09-27
        • 1970-01-01
        • 1970-01-01
        • 2013-08-24
        • 2021-03-12
        • 1970-01-01
        相关资源
        最近更新 更多