【发布时间】:2016-12-07 21:22:57
【问题描述】:
我正在创建一种类似终端的视图,它将显示通过蓝牙接收和传输的消息,例如收到的消息是蓝色的,发送的消息是红色的。
到目前为止,我设法将 textView 放在 scrollView 中,并通过使用 .append() 方法在滚动视图中添加行。
布局如下:
<ScrollView
android:id="@+id/scrollingView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/disconnectButton"
android:layout_alignParentStart="true">
<TextView
android:text="Received and sent txt!!!\n"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/scrollTextView"
android:minLines="11"
android:maxLines="11"/>
</ScrollView>
以及向scrollView添加文本的代码:
scrollView = (ScrollView) findViewById(R.id.scrollingView);
scrollTextView = (TextView) findViewById(R.id.scrollTextView);
scrollTextView.setGravity(Gravity.BOTTOM); scrollTextView.setTextColor(getResources().getColor(R.color.receivedBTColor));
scrollTextView.append("$"+dataInPrint+"\n");
scrollView.fullScroll(View.FOCUS_DOWN);
问题是每条线都应该能够有不同的颜色。 setTextColor() 方法为整个 textView 设置颜色,这完全不是我想要的,因为我需要临时保存向上移动的行,直到 scrollView 溢出。我看了使用 Spannable 类的例子,但是比较乱。
谁能提出一种制作可滚动彩色文本的方法?像这样的东西? Example from Bluetooth terminal app
非常感谢!
【问题讨论】:
标签: android text colors terminal scrollview