2013-09-26 27 views
8

我想顯示一段長文本如下。這裏是來自Flipboard的快照。如何在TextView中使第一個字符比其他字符大得多

enter image description here

爲了達到這樣的效果,我試圖用2 TextView秒。

<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行不是最左邊的。第二行和第三行最左邊的空間由大字體TextView佔據。

enter image description here

有沒有更好的技術,我可以使用,比如一個在Flipboard的的?

+3

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

+3

只需使用'HTML'標籤和使用'HTML載入您的數據。 fromHtml()'在'TextView'中或者在'WebView'中。 – user370305

+1

@Andrew Spannable文本是一個好主意。謝謝。我補充說,作爲答案。 –

回答

5

通過使用單個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); 
0

對於我可能你已經使用3個TextViews: 1 - 段落號 2 - 第一行和第二行 3 - 文本的其餘部分。

+0

但是在您的Java代碼中,如何確定要在哪些文本視圖中放置哪些內容? –

+0

在其中使用WebView是很好的,但我可能不會這樣做。你需要找出多少文本將填滿前兩個TextView,並在它減去字符串後。檢查此:http://stackoverflow.com/questions/3630086/how-to-get-string-width-on-android和http://stackoverflow.com/questions/5864159/count-words-in-a-string -方法 。 – goodm

0

試試這個。

佈局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類

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); 
     } 
    }); 
} 
} 
相關問題