繁体   English   中英

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

[英]How to change the font size of an textview element in Android?

我试图在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: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。

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

import android.text.Html;

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

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

使用此链接中的HTML类http://code.google.com/p/android-richtexteditor/source/browse/trunk/src/net/sgoliver/Html.java?r=4 我们可以从这个类中设置字体大小。 正常的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));

您可以像这样使用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>"))

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

您应该在XML中的文本视图标记中添加行:

android:textSize="32sp"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM