如何更改Android中textview元素的字体大小?

Beg*_*ner 9 android textview android-layout

我试图在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>
Run Code Online (Sandbox Code Playgroud)

Ags*_*gs1 16

使用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"/>
Run Code Online (Sandbox Code Playgroud)

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

  • 是不是我们需要使用SP的字体大小?我认为它应该是40sp而不是40dp (2认同)
  • 如果用户可以在不破坏UI的情况下重新缩放文本,请使用sp.如果重新缩放文本会破坏UI,请使用dp.http://stackoverflow.com/a/11638914/2713250 (2认同)

pel*_*lus 8

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

import android.text.Html;

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

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