2013-09-26 118 views
8

Tôi muốn hiển thị một đoạn văn bản dài như sau. Đây là ảnh chụp nhanh từ Flipboard.Làm cách nào để làm cho ký tự đầu tiên lớn hơn nhiều trong một TextView

enter image description here

Để đạt được hiệu quả như vậy, tôi đã cố gắng để sử dụng 2 TextView s.

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

Tôi nhận được kết quả như sau.

Khá gần. Nhưng không phải những gì tôi muốn. Điều này là do dòng thứ 2 và dòng thứ 3 không được căn chỉnh ở bên trái nhiều nhất. Không gian bên trái nhiều nhất cho dòng thứ 2 và dòng thứ 3, bị chiếm bởi TextView với phông chữ lớn.

enter image description here

Có bất kỳ kỹ thuật tốt hơn tôi có thể sử dụng, chẳng hạn như một trong Flipboard không?

+3

http://stackoverflow.com/questions/2159847/is-there-any-example-about-spanned-and-spannable-text – Andrew

+3

Chỉ cần sử dụng 'thẻ HTML' và tải dữ liệu của bạn với' Html. fromHtml() 'trong' TextView' hoặc trong 'WebView'. – user370305

+1

@Andrew Văn bản có thể nói là một ý tưởng tuyệt vời. Cảm ơn. Tôi đã thêm nó làm câu trả lời. –

Trả lời

5

Bằng cách sử dụng một đơn TextView với BufferType.SPANNABLE sẽ giải quyết vấn đề.

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

Đối với tôi, có lẽ bạn đã sử dụng 3 TextViews: 1 - Đoạn văn 2 - dòng đầu tiên và dòng thứ hai 3 - phần còn lại của văn bản.

+0

Nhưng trong mã Java của bạn, làm thế nào bạn có thể xác định nội dung nào để đặt ở chế độ xem văn bản nào? –

+0

Thật tuyệt khi sử dụng WebView trong đó, nhưng có lẽ tôi sẽ không làm điều này. Bạn cần phải tìm hiểu bao nhiêu văn bản sẽ lấp đầy 2 TextView đầu tiên và sau khi nó trừ chuỗi. Kiểm tra điều này: http://stackoverflow.com/questions/3630086/how-to-get-string-width-on-android và http://stackoverflow.com/questions/5864159/count-words-in-a-string -phương pháp . – goodm

0

Hãy thử điều này.

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> 

Hoạt động lớp

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