如何使第一个字符比TextView中的其他字符大得多 [英] How to make the first character much larger than other in a TextView

查看:83
本文介绍了如何使第一个字符比TextView中的其他字符大得多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一段长文本,如下所示.这是Flipboard的快照.

I would like to display a paragraph of long text as follow. Here is the snapshot from Flipboard.

为了达到这种效果,我尝试使用2个TextView s.

In order to achieve such effect, I tried to use 2 TextViews.

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40sp"
        android:text="T" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="76dp"
        android:text="op British supermarket Tesco (LSE: TSCO) (NASDAQOTH: TSCDY.US) is due to announce its half year results on" />

</LinearLayout>

我得到以下结果.

非常接近.但不是我想要的.这是因为第二行和第三行未最左对齐.第二行和第三行最左边的空间由带有大字体的TextView占据.

Quite close. But not what I want. This is because the 2nd line and the 3rd line is not align to the left most. The left most space for 2nd line and 3rd line, is occupied by the TextView with large font.

还有什么更好的技术可以使用,例如Flipboard的技术?

Is there any better technique I can use, such as the one in Flipboard's?

推荐答案

通过将单个TextViewBufferType.SPANNABLE一起使用将解决此问题.

By using a single TextView with BufferType.SPANNABLE will solve the problem.

final SpannableString spannableString = new SpannableString(title);
int position = 0;
for (int i = 0, ei = title.length(); i < ei; i++) {
    char c = title.charAt(i);
    if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) {
        position = i;
        break;
    }
}
spannableString.setSpan(new RelativeSizeSpan(2.0f), position, position + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//titleTextView.setText(spannableString.toString());
titleTextView.setText(spannableString, BufferType.SPANNABLE);

这篇关于如何使第一个字符比TextView中的其他字符大得多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆