textview每个文字加边框
时间: 2024-01-21 20:04:24 浏览: 19
要在TextView中为每个文字添加边框,你可以使用SpannableString和AbsoluteSizeSpan类的结合。
你可以按照以下步骤进行操作:
1. 创建一个SpannableString对象,并将其设置为TextView的文本。
2. 为每个字符创建一个AbsoluteSizeSpan对象,并将其设置为SpannableString。
3. 使用ForegroundColorSpan类为每个字符设置边框颜色。
以下是一个示例代码:
```
String text = "Hello World";
SpannableString spannableString = new SpannableString(text);
for (int i = 0; i < text.length(); i++) {
AbsoluteSizeSpan absoluteSizeSpan = new AbsoluteSizeSpan(24, true);
ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.BLACK);
spannableString.setSpan(absoluteSizeSpan, i, i + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(foregroundColorSpan, i, i + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
textView.setText(spannableString);
```
在上面的代码中,我们创建了一个SpannableString对象,并将其设置为TextView的文本。接下来,我们使用循环为每个字符创建一个AbsoluteSizeSpan对象,并使用ForegroundColorSpan类为每个字符设置边框颜色。最后,我们将SpannableString对象设置为TextView的文本。
请注意,上面的代码只是一个示例,你可以根据你的需求进行更改。