2

我正在创建一个水平字符串,在每个文本之间都有彩色项目符号。我使用 Spanned StringBuilder 并返回值为 Html.fromHtml(builder.toString()) 的方法。问题是如何使用该返回值在 TextView 中设置文本。

public class MainActivity extends AppCompatActivity {

    TextView text;
    StringBuilder builder;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = (TextView)findViewById(R.id.text);
        text.setText(Html.fromHtml(builder.toString()));

    }

    Spanned createSpannable(List<String> interests) {
        builder = new StringBuilder();
        int size = interests.size();
        for (int i = 0; i < size; i++) {
            builder.append(interests.get(i));
            if (i < size - 1) {
                builder.append(" <font color=\"#c52d08\">&#8226</font> ");
            }
        }

        return Html.fromHtml(builder.toString());
    }

}
4

1 回答 1

3

试试这个:

public class MainActivity extends AppCompatActivity {

    TextView text;
    StringBuilder builder;
    Spanned mSpanned;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = (TextView)findViewById(R.id.text);
        mSpanned = createSpannable(YourList); //Pass your list
        text.setText(mSpanned);

    }

    Spanned createSpannable(List<String> interests) {
        builder = new StringBuilder();
        int size = interests.size();
        for (int i = 0; i < size; i++) {
            builder.append(interests.get(i));
            if (i < size - 1) {
                builder.append(" <font color=\"#c52d08\">&#8226</font> ");
            }
        }
        return Html.fromHtml(builder.toString());
    }

}
于 2016-02-19T11:42:31.097 回答