字符串数组文本格式 [英] String array text formatting

查看:192
本文介绍了字符串数组文本格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  String [] text = {Address 1:Street nr.45,
Address 2:Street nr.67,
Address 3:Street nr。56 \\\
电话号码:000000000};

以后会用到:

<$ p ((TextView)findViewById(R.id.adresa))。setText(text [newSelectedAddress]);

当从微调器中选择一个项目时。

如何添加格式到字符串中的文本?我想要地址与大胆,街道号码。与斜体

解决方案

您必须使用 Spannable s。虽然创造它们有点罗嗦,看起来很复杂,但这不是火箭科学。
一个例子:

  String source =This is example text; 
SpannedString out = new SpannedString(source);
StyleSpan boldSpan =新StyleSpan(Typeface.BOLD);
StyleSpan boldSpan2 =新StyleSpan(Typeface.BOLD);
out.setSpan(boldSpan,1,3,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
out.setSpan(boldSpan2,9,12,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

结果:T hi 是e xam ple text



然后你 setText 这个Spannable而不是普通的String。


I have this String:

    String[] text = {"Address 1: Street nr.45 ",
         "Address 2: Street nr.67",
         "Address 3: Street nr. 56 \n Phone number: 000000000"};

Which gets used later by:

        ((TextView)findViewById(R.id.adresa)).setText(text[newSelectedAddress]);

when selecting an item from a spinner.

How can I add formatting to the text inside the string?? I want Address to be with bold, Street nr. with Italic

解决方案

You have to use Spannables. Although creating them is a bit wordy and seems complex, it isn't rocket science. An example:

String source = "This is example text";
SpannedString out = new SpannedString(source);
StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
StyleSpan boldSpan2 = new StyleSpan(Typeface.BOLD);
out.setSpan(boldSpan, 1, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
out.setSpan(boldSpan2, 9, 12, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

Result: This is example text

You then setText this Spannable instead of the normal String.

这篇关于字符串数组文本格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆