【问题标题】:How to make the first character much larger than other in a TextView如何使第一个字符比 TextView 中的其他字符大得多
【发布时间】:2013-10-02 13:48:27
【问题描述】:

我想显示一段长文本如下。这是来自 Flipboard 的快照。

为了达到这样的效果,我尝试使用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>

我得到以下结果。

非常接近。但不是我想要的。这是因为第 2 行和第 3 行最左对齐。第2行和第3行最左边的空间被TextView占据,大字体。

有没有更好的技术可以使用,比如 Flipboard 中的那种?

【问题讨论】:

  • 只需使用HTML 标签并在TextViewWebView 中使用Html.fromHtml() 加载您的数据。
  • @Andrew Spannable text 是个好主意。谢谢。我将其添加为答案。

标签: android


【解决方案1】:

通过使用单个TextViewBufferType.SPANNABLE 将解决问题。

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);

【讨论】:

    【解决方案2】:

    对我来说,您可能已经使用了 3 个 TextView: 1 - 段落字母 2 - 第一行和第二行 3 - 文本的其余部分。

    【讨论】:

    【解决方案3】:

    试试这个。

    布局 XML

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal" >
    
    <TextView
        android:id="@+id/bigChar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:gravity="center_vertical"
        android:text="T"
        android:textSize="40sp" />
    
    <TextView
        android:id="@+id/sideText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_toRightOf="@id/bigChar"
        android:gravity="center_vertical"
        android:maxLines="2" />
    
    
    <TextView
        android:id="@+id/spillText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/bigChar"
        android:layout_alignParentLeft="false"
        android:layout_below="@id/bigChar"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="-5dp"
        android:gravity="center_vertical" />
    </RelativeLayout>
    

    活动类

    public class MainActivity extends Activity {
    private String inputString = "op British supermarket Tesco (LSE: TSCO) (NASDAQOTH: TSCDY.US) is due to announce its half year results on and this is the extra bit of spill text";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
    
        TextView bigChar = (TextView) findViewById(R.id.bigChar);
        final TextView sideText = (TextView) findViewById(R.id.sideText);
        final TextView spillText = (TextView) findViewById(R.id.spillText);
    
    
        ViewTreeObserver vto = sideText.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
               Layout layout = sideText.getLayout();  
               int numOfLines = layout.getLineEnd(2);
    
               // split the string now
               String spillTextString = inputString.substring(numOfLines, inputString.length());
               spillText.setText(spillTextString);
            }
        });
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-05
      • 2020-06-12
      • 2020-06-21
      • 2020-01-10
      • 1970-01-01
      相关资源
      最近更新 更多