Android ScrollView 中的 TextView - 每个新行的不同颜色
Android TextView in ScrollView - Different color for each new line
我正在创建一种类似于终端的视图,它将显示通过蓝牙接收和传输的消息,例如蓝色接收消息,红色发送消息。
到目前为止,我设法将 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的例子class,但是比较乱
谁能推荐一种制作可滚动彩色文本的方法?是这样的吗?
Example from Bluetooth terminal app
非常感谢!
您可以利用 HTML 和 Android 的 Html.fromHtml()
...
进行一些小技巧编辑
String dataInPrint = "" //Whatever the output message is.
String redB = "<font color="red">";
String blueB = "<font color="blue">";
String colorEnd = "</font>";
if(//message is send){
yourtextview.append(Html.fromHtml(redB + dataInPrint + colorEnd));
}
else{//message is recieve
yourtextview.append(Html.fromHtml(blueB + dataInPrint + colorEnd));
}
我正在创建一种类似于终端的视图,它将显示通过蓝牙接收和传输的消息,例如蓝色接收消息,红色发送消息。
到目前为止,我设法将 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的例子class,但是比较乱
谁能推荐一种制作可滚动彩色文本的方法?是这样的吗? Example from Bluetooth terminal app
非常感谢!
您可以利用 HTML 和 Android 的 Html.fromHtml()
...
String dataInPrint = "" //Whatever the output message is.
String redB = "<font color="red">";
String blueB = "<font color="blue">";
String colorEnd = "</font>";
if(//message is send){
yourtextview.append(Html.fromHtml(redB + dataInPrint + colorEnd));
}
else{//message is recieve
yourtextview.append(Html.fromHtml(blueB + dataInPrint + colorEnd));
}