更改android中textview元素的字体大小

时间:2022-11-11 13:16:26

I am trying to display 2 items in a textview. Is their any way to change the font of the single item in a textview ?.

我试图在textview中显示两个条目。他们有没有办法改变文本视图中单个条目的字体?

here is the XML which i am using

这是我正在使用的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>

Thanks :)

谢谢:)

4 个解决方案

#1


16  

Use android:textSize.

使用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"/>

Use sp if the user can rescale the text without breaking the UI. If rescaling the text would break the UI, use dp.

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

#2


5  

One way is to use TextView.setText() method and feed it with HTML, like this:

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

import android.text.Html;

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

I often use it for some minor markup (like make part bolder or smaller)

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

#3


2  

Use the HTML class from this link http://code.google.com/p/android-richtexteditor/source/browse/trunk/src/net/sgoliver/Html.java?r=4 . We would be able to set font size from this class. Normal android.text.Html actually ignores font size. Tried and tested and it worked for me.

使用这个链接中的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));

#4


0  

You can use html for this like this:

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

remove color and style attributes if you don't need them.

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

#1


16  

Use android:textSize.

使用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"/>

Use sp if the user can rescale the text without breaking the UI. If rescaling the text would break the UI, use dp.

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

#2


5  

One way is to use TextView.setText() method and feed it with HTML, like this:

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

import android.text.Html;

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

I often use it for some minor markup (like make part bolder or smaller)

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

#3


2  

Use the HTML class from this link http://code.google.com/p/android-richtexteditor/source/browse/trunk/src/net/sgoliver/Html.java?r=4 . We would be able to set font size from this class. Normal android.text.Html actually ignores font size. Tried and tested and it worked for me.

使用这个链接中的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));

#4


0  

You can use html for this like this:

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

remove color and style attributes if you don't need them.

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