我正在创建一个水平字符串,每个文本之间都有彩色的项目符号点。我使用的方法是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());
    }

}

最佳答案

试试这个:

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

}

关于android - 从返回值设置textview,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35504375/

10-12 06:29