How to make the first character much larger than other in a TextView

后端 未结 3 1608
谎友^
谎友^ 2020-12-16 23:51

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

\"enter

相关标签:
3条回答
  • 2020-12-17 00:05

    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);
    
    0 讨论(0)
  • 2020-12-17 00:14

    Try this.

    Layout 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>
    

    Activity class

    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);
            }
        });
    }
    }
    
    0 讨论(0)
  • 2020-12-17 00:22

    For me probably you have use 3 TextViews: 1 - Paragraph Letter 2 - first and second line 3 - rest of the text.

    0 讨论(0)
提交回复
热议问题