8

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

在此处输入图像描述

为了达到这样的效果,我尝试使用TextView2s。

<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 中的那种?

4

3 回答 3

5

通过使用单个TextViewwithBufferType.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);
于 2013-09-29T17:24:32.747 回答
0

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

于 2013-09-26T14:22:15.850 回答
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>

活动课

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);
        }
    });
}
}
于 2013-09-26T15:26:54.363 回答