在android中用彩色背景将两个文本一行接一行地放在一起



我有两个文本(大量),并希望在一个活动中显示它们,一行一行,彼此之间,就像在代码中那样,实际上这两个文本,是一个文本及其翻译到另一种语言。我写的每一行单独的TextView这样做,我知道这不是正确和有效的方式,因为它不能支持不同的屏幕尺寸,需要大量的TextView的大文本。我在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"/>

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

示例来自这里

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

相关内容

  • 没有找到相关文章

最新更新