在android中用彩色背景逐行放置两个文本

时间:2015-05-28 15:06:04

标签: android text textview multiline

我有两个文本(大量)并且希望在一个活动中逐行显示它们,在代码之间相互之间显示它们,实际上这两个文本是一个文本及其翻译成其他语言。 我在单独的TextView中编写每一行来做到这一点,我知道它不是正确有效的方法,因为它不能支持不同的屏幕尺寸,需要大量的TextViews用于大文本。 我在activity_layout中的代码是:

    <TextView android:text="@string/En_Line1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#AADDBB"
        android:textSize="20sp"/>
    <TextView android:text="@string/Sp_Line1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#BBBBBB"
        android:textSize="20sp"/>
    <TextView android:text="@string/En_Line2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#AADDBB"
        android:textSize="20sp"/>
    <TextView android:text="@string/Sp_Line2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#BBBBBB"
        android:textSize="20sp"/>
    <TextView android:text="@string/En_Line3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#AADDBB"
        android:textSize="20sp"/>
    <TextView android:text="@string/Sp_Line3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#BBBBBB"
        android:textSize="20sp"/>
    <TextView android:text="@string/En_Line4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#AADDBB"
        android:textSize="20sp"/>
    <TextView android:text="@string/Sp_Line4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#BBBBBB"
        android:textSize="20sp"/>
    <TextView android:text="@string/En_Line5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#AADDBB"
        android:textSize="20sp"/>
    <TextView android:text="@string/Sp_Line5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#BBBBBB"
        android:textSize="20sp"/>
    <TextView android:text="@string/En_Line6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#AADDBB"
        android:textSize="20sp"/>
    <TextView android:text="@string/Sp_Line6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#BBBBBB"
        android:textSize="20sp"/>
<TextView android:text="@string/En_Line7"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#AADDBB"
    android:textSize="20sp"/>
<TextView android:text="@string/Sp_Line7"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#BBBBBB"
    android:textSize="20sp"/>
<TextView android:text="@string/En_Line8"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#AADDBB"
    android:textSize="20sp"/>
<TextView android:text="@string/Sp_Line8"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#BBBBBB"
    android:textSize="20sp"/>

2 个答案:

答案 0 :(得分:0)

使用Spannable String

  // this is the text we'll be operating on
    SpannableString text = new SpannableString("Lorem ipsum dolor sit amet");

    // make "Lorem" (characters 0 to 5) red
    text.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0);

    // make "ipsum" (characters 6 to 11) one and a half time bigger than the textbox
    text.setSpan(new RelativeSizeSpan(1.5f), 6, 11, 0);

    // make "dolor" (characters 12 to 17) display a toast message when touched
    final Context context = this;
    ClickableSpan clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(View view) {
            Toast.makeText(context, "dolor", Toast.LENGTH_LONG).show();
        }
    };
    text.setSpan(clickableSpan, 12, 17, 0);

Example is from here

答案 1 :(得分:0)

String source = "This is example text";
Spannable out = new SpannedString(source);
StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
StyleSpan boldSpan2 = new StyleSpan(Typeface.BOLD);
out.setSpan(boldSpan, 1, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
out.setSpan(boldSpan2, 9, 12, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);