【问题标题】:How to change the font size of an textview element in Android?如何在 Android 中更改 textview 元素的字体大小?
【发布时间】:2013-03-20 12:58:04
【问题描述】:

我正在尝试在 TextView 中显示 2 个项目。他们有什么方法可以更改 TextView 中单个项目的字体吗?

这是我正在使用的 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:gravity="center_horizontal"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:id="@+id/Rowtext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:text="Listiems"
        android:background="@drawable/customshape"
         />

</LinearLayout>

【问题讨论】:

标签: android android-layout textview


【解决方案1】:

使用 android:textSize。

<TextView
    android:id="@+id/time"
    android:gravity="right"
    android:padding="5dp"
    android:textSize="40dp"
    android:textColor="#88ffff00"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Time: 60"/>

如果用户可以在不破坏 UI 的情况下重新缩放文本,请使用 sp。如果重新缩放文本会破坏 UI,请使用 dp。

【讨论】:

  • 字体大小不是需要用SP吗?我认为应该是 40sp 而不是 40dp
  • 如果用户可以在不破坏 UI 的情况下重新缩放文本,请使用 sp。如果重新缩放文本会破坏 UI,请使用 dp. stackoverflow.com/a/11638914/2713250
【解决方案2】:

一种方法是使用 TextView.setText() 方法并用 HTML 提供它,如下所示:

import android.text.Html;

String n = "<b>bold</b> <small>small</small>";
TextView tv = (TextView) findViewById(...)
tv.setText(Html.fromHtml(n));

我经常用它来做一些小的标记(比如让部分变粗或变小)

【讨论】:

  • erm,问题是关于更改字体:-/ Shade 的答案看起来不错!
  • “fromHtml”方法中有错字。它改为 formHtml。
【解决方案3】:

使用此链接 http://code.google.com/p/android-richtexteditor/source/browse/trunk/src/net/sgoliver/Html.java?r=4 中的 HTML 类。我们可以从这个类中设置字体大小。普通的 android.text.Html 实际上会忽略字体大小。尝试和测试,它对我有用。

TextView tv = (TextView)findViewById(R.id.textview);

String s = "<p>Some Text here<br><b>hi </b><font size =\"20\"><b><i>\"italics</i></b></font><b><i> </i></b><b><i><u>underline</u></i></b></p>";

tv.setText(Html.fromHtml(s));

【讨论】:

    【解决方案4】:

    您可以像这样使用 html:

    mytextView.setText(Html.fromHtml("<p> <font size="20" color="#0066FF" style="font-style:italic">Push this button</font> to start a new game.</p>"))
    

    如果不需要,请删除颜色和样式属性。

    【讨论】:

      【解决方案5】:

      您应该在 XML 中的 Text View Tag 中添加一行:

      android:textSize="32sp"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-27
        • 2012-05-08
        • 2020-10-17
        • 2020-09-03
        • 2014-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多