English 中文(简体)
我能否选择每个文本的字句一对案文一的颜色。
原标题:Can I select a color for each text line I append to a TextView?

我有一份案文,将作为蓝色通道。 当我发出指挥时,我希望以肤色(例如cyan)来写,并用不同颜色(例如红色)得到的答复。

这样做是可能的,如果是,如何?

我读到,可能采用超文本处理,但我不相信这是最佳办法,甚至可以如何这样做。

最佳回答

您是否真的需要是书面意见,还是能够使用清单,并在每个指挥/答复的名单中增加新的一行?

really使用案文 你们可以这样做(这是你可以做的榜样,你可以照抄和照抄你们的遗忘):

package com.c0deattack;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.style.ForegroundColorSpan;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MultipleColoursInOneTextViewActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TextView tv = new TextView(this);

        String command = "This is a command";
        String response = "
This is a response";

        tv.append(command + response);
        Spannable spannableText = (Spannable) tv.getText();
        spannableText.setSpan(new ForegroundColorSpan(Color.GREEN), 0, command.length(), 0);
        spannableText.setSpan(new ForegroundColorSpan(Color.RED), command.length(), command.length() + response.length(), 0);

        LinearLayout layout = new LinearLayout(this);
        layout.addView(tv);
        setContentView(layout);
    }
}

因此,这表明可以这样做,但你显然要注意到,在每一指挥/电塔开始和结束时,你必须打断线断线和停工,以便你能够对其适用正确的颜色。 这对我来说不是那么困难,而是感觉到cl。

问题回答

在这方面,根据C0deAttack的回答,没有什么帮助作用,这就简化了事情。

public static void appendColoredText(TextView tv, String text, int color) {
    int start = tv.getText().length();
    tv.append(text);
    int end = tv.getText().length();

    Spannable spannableText = (Spannable) tv.getText();
    spannableText.setSpan(new ForegroundColorSpan(color), start, end, 0);
}

仅取代任何呼吁

textView.append("Text")

iii

appendColoredText(textView, "Text", Color.RED);

如果你使用:

textView.append(Html.fromHtml("<font color= #FFFFFF </font>"));
public static void appendColoredText(TextView tv, String text, int color) {
    SpannableStringBuilder ssb = new SpannableStringBuilder();
    ssb.append(text);
    ssb.setSpan(new ForegroundColorSpan(color), 0, text.length(), 0);
    tv.append(ssb);
}

优化 回答

我们可以创建<代码>SpannableStringBuilder 反对持有textcolor%20/code>,并使用append(setSpan() 添加案文和彩色字体的>方法。

This will be more efficient, as it avoids creating a new Spannable object every time the method is called.





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...