编码很新,只需几周的时间在这里和那里学习。我正在尝试构建一个应用程序,并且正在取得进展,但却遇到了障碍。我想在我的子项中只有两个单词粗体。有帮助吗?这是我的java文件,其中包含可扩展列表的详细信息。
Appliances.add("The first word of each paragraph\nis what I would like to be bold faced");
答案 0 :(得分:0)
final SpannableStringBuilder sb = new SpannableStringBuilder("The first word of each paragraph, what I would like to be bold faced.");
// Span to make text bold
final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD);
// make all characters of the second and sixth word Bold
sb.setSpan(bss, 4, 9, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make word 'first' bold
sb.setSpan(bss, 23, 32, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make word 'paragraph' bold
sb
应该生成字符串 - 每个段的第一个字,我希望大胆面对。
然后根据需要使用字符串:
Appliances.add(sb.toString());
答案 1 :(得分:0)
有一个最简单的解决方案,只需将您的字符串添加到" res"下的值文件夹的string.xml文件中。安卓工作室的文件夹。
for make two words bold只需将此字符串添加到上面的stirng.xml文件中。
<b>The</b> first word of each paragraph\nis <b>what</b> I would like to be bold faced
然后像使用它一样。
Appliances.add(R.string.strinname);
答案 2 :(得分:0)
使用以下类别,即
yourTextView.setText(new RichTextView(sentence).setBold(wordWichYouwantBold))
你可以在里面找到另一个有用的方法:)享受!!!
import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.view.View;
/**
* Created by RajeshKushvaha on 19-11-16
*/
//.setText(text, TextView.BufferType.SPANNABLE); to textView if not work
public class RichTextView extends SpannableString {
private String syntax;
public RichTextView(String syntax) {
super(syntax);
this.syntax = syntax;
}
public RichTextView setTextColor(String word, int color) {
setSpan(new ForegroundColorSpan(color), syntax.indexOf(word), syntax.indexOf(word) + word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return this;
}
public RichTextView setSize(String word, float howMuch) {
setSpan(new RelativeSizeSpan(howMuch), syntax.indexOf(word), syntax.indexOf(word) + word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return this;
}
public RichTextView setStrikeOut(String word) {
setSpan(new StrikethroughSpan(), syntax.indexOf(word), syntax.indexOf(word) + word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return this;
}
public RichTextView setUrl(String word, String redirectUrl) {
setSpan(new URLSpan(redirectUrl), syntax.indexOf(word), syntax.indexOf(word) + word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return this;
}
public RichTextView setBold(String word) {
StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
setSpan(boldSpan, syntax.indexOf(word), syntax.indexOf(word) + word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return this;
}
//setMovementMethod(LinkMovementMethod.getInstance()); after or before call
public RichTextView setClickable(String word, final setOnLinkClickListener listener) {
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View view) {
if (listener != null) {
listener.onLinkClicked();
}
}
};
setSpan(clickableSpan, syntax.indexOf(word), syntax.indexOf(word) + word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return this;
}
public interface setOnLinkClickListener {
public void onLinkClicked();
}
}