在 Android TextView 中设置不同的文本大小

Set different text size in Android TextView

我有一个只有 1 行文本的 TextView。现在我必须多加一行来设置用户的状态。所以我设置 属性 android:lines="2"

文本视图看起来像:

<TextView
    android:id="@+id/providerName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1.0"
    android:ellipsize="marquee"
    android:fontFamily="sans-serif-light"
    android:lines="2"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textStyle="bold" />

我添加文字的方式是这样的:

mNameAndStatus.setText(providerNameText +  "\n" + mUserStatus);

图片实际情况如何。

问题是第一行必须是 Large 文本,但第二行必须是 mediumsmall 不知道可不可以

我怎样才能做到这一点?

你需要做这样的事情:

mNameAndStatus.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24);

您的文字大小以 sp 为单位(示例中为 24sp)。

这里是全部TypedValuehttp://developer.android.com/reference/android/util/TypedValue.html

尝试

String asHtml = firstLine+"<br/><small>"+secondLine+"</small>";
textView.setText(Html.fromHtml(asHtml));

或简单地使用 Spannable (AbsoluteSizeSpan)