
这是我的代码:
String fullname = "some name"; String searchFreeText = "some"; SpannableString wordToSpan = new SpannableString(fullname);Typeface helvticaBoldTypeface = Typeface.createFromAsset(context.getAssets(),Font_HELVETICA_BolD);int startposition = fullname.tolowerCase().indexOf(searchFreeText.tolowerCase());StyleSpan bold = new StyleSpan(helvticaBoldTypeface.getStyle()); wordToSpan.setSpan(bold,startposition,startposition + searchFreeText.length(),0); firstlineTextVIEw.setText(wordToSpan,BufferType.SPANNABLE);
文字仍然不粗体.
如果我将代码更改为:
firstlineTextVIEw.setTypeface(helvticaBoldTypeface,Typeface.BolD);
并删除
StyleSpan bold = new StyleSpan(helvticaBoldTypeface.getStyle()); wordToSpan.setSpan(bold,0);
然后所有的文字都是粗体,这没关系,但不是我想要的.
我只需要使用大胆的特定文本.
我希望看到这个:一些名字.
问题是什么?
谢谢
I noticed one thing:
You are using Font_HELVETICA_BolD (Which is already bold so nothing special gonna happen to your text cuz it is already bold just change it to some regular Font)
You can find some fonts here
For making specific text bold and colored you can try the following code
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(textToBeSpanneDWithBold);StyleSpan styleSpan = new StyleSpan(androID.graphics.Typeface.BolD);spannableStringBuilder.setSpan(styleSpan,positionOfSearchedText,positionOfSearchedText + searchTextToBeBold.length(),Spannable.SPAN_INCLUSIVE_INCLUSIVE);//Use this if you wanna make specific text boldSpannableString uncolouredSpan = new SpannableString(spannableStringBuilder);textVIEwToBeBoldSpanned.setText(uncolouredSpan);//Use this if you wanna make specific text bold plus colouredSpannableString coloredSpan = giveMecoloredSpan( new SpannableString(spannableStringBuilder),ContextCompat.getcolor(getContext(),R.color.your_specific_colour));textVIEwToBeBoldSpannedAndColoured.setText(coloredSpan);public static SpannableString giveMecoloredSpan(SpannableString spannableString,int color) { spannableString.setSpan( new ForegroundcolorSpan(color),spannableString.length(),0); return spannableString;} 总结If Still somehow you are stuck with your problem for that i have created a repo on Github 07001
活动代码来自我的回购
CustomTextVIEwActivity.java
import androID.support.v4.content.ContextCompat;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.text.Spannable;import androID.text.SpannableString;import androID.text.SpannableStringBuilder;import androID.text.style.ForegroundcolorSpan;import androID.text.style.StyleSpan;import androID.vIEw.VIEw;import androID.Widget.button;public class CustomTextVIEwActivity extends AppCompatActivity implements VIEw.OnClickListener { public static final String MAKE_THIS_SPECIFIC_PART_colorED = "MakeThisspecificPartcolored"; public static final String MAKE_THIS_SPECIFIC_PART_BolD = "MakeThisspecificPartBold"; public static final String MAKE_THIS_SPECIFIC_PART_BolD_AND_colorED = "MakeThisspecificPartBoldAndcolored"; CustomTextVIEw customTextVIEw1,customTextVIEw2,customTextVIEw3; button changetoRoboto,changetoCalculus,makeSpecificTextcolored,makeSpecificTextBold,makeSpecificTextBoldAndcolored; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_text_vIEw); customTextVIEw1 = (CustomTextVIEw) findVIEwByID(R.ID.customTextVIEw1); customTextVIEw2 = (CustomTextVIEw) findVIEwByID(R.ID.customTextVIEw2); customTextVIEw3 = (CustomTextVIEw) findVIEwByID(R.ID.customTextVIEw3); changetoRoboto = (button) findVIEwByID(R.ID.change_to_roboto); changetoCalculus = (button) findVIEwByID(R.ID.change_to_calculus); makeSpecificTextBold = (button) findVIEwByID(R.ID.make_specific_text_bold); makeSpecificTextcolored = (button) findVIEwByID(R.ID.make_text_colored); makeSpecificTextBoldAndcolored = (button) findVIEwByID(R.ID.make_text_bold_and_colored); changetoRoboto.setonClickListener(this); changetoCalculus.setonClickListener(this); makeSpecificTextcolored.setonClickListener(this); makeSpecificTextBold.setonClickListener(this); makeSpecificTextBoldAndcolored.setonClickListener(this); } @OverrIDe public voID onClick(VIEw v) { switch (v.getID()) { case R.ID.change_to_roboto: customTextVIEw1.setFont(getString(R.string.Font_roboto_regular)); customTextVIEw2.setFont(getString(R.string.Font_roboto_regular)); customTextVIEw3.setFont(getString(R.string.Font_roboto_regular)); break; case R.ID.change_to_calculus: customTextVIEw1.setFont(getString(R.string.Font_calculus_sans)); customTextVIEw2.setFont(getString(R.string.Font_calculus_sans)); customTextVIEw3.setFont(getString(R.string.Font_calculus_sans)); break; case R.ID.make_text_colored: makeTextColoured(); break; case R.ID.make_specific_text_bold: makeSpecificTextBold(); break; case R.ID.make_text_bold_and_colored: makeTextBoldAndcolored(); break; } } public static SpannableString giveMecoloredSpan(SpannableString spannableString,int color) { spannableString.setSpan( new ForegroundcolorSpan(color),0); return spannableString; } private voID makeTextColoured() { String textToBeSpanned = customTextVIEw1.getText().toString(); SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(textToBeSpanned); int positionOfSearchedText = textToBeSpanned.indexOf(MAKE_THIS_SPECIFIC_PART_colorED); SpannableString coloredSpan = giveMecoloredSpan( new SpannableString(textToBeSpanned),ContextCompat.getcolor(getApplicationContext(),androID.R.color.holo_red_dark)); customTextVIEw1.setText(coloredSpan); } private voID makeSpecificTextBold() { String textToBeSpanned = customTextVIEw2.getText().toString(); SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(textToBeSpanned); int positionOfSearchedText = textToBeSpanned.indexOf(MAKE_THIS_SPECIFIC_PART_BolD); StyleSpan styleSpan = new StyleSpan(androID.graphics.Typeface.BolD); spannableStringBuilder.setSpan(styleSpan,positionOfSearchedText + MAKE_THIS_SPECIFIC_PART_BolD.length(),Spannable.SPAN_INCLUSIVE_INCLUSIVE); customTextVIEw2.setText(spannableStringBuilder); } private voID makeTextBoldAndcolored() { String textToBeSpanned = customTextVIEw3.getText().toString(); SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(textToBeSpanned); int positionOfSearchedText = textToBeSpanned.indexOf(MAKE_THIS_SPECIFIC_PART_BolD_AND_colorED); StyleSpan styleSpan = new StyleSpan(androID.graphics.Typeface.BolD); spannableStringBuilder.setSpan(styleSpan,positionOfSearchedText + MAKE_THIS_SPECIFIC_PART_BolD_AND_colorED.length(),Spannable.SPAN_INCLUSIVE_INCLUSIVE); SpannableString coloredBoldSpan = giveMecoloredSpan( new SpannableString(spannableStringBuilder),androID.R.color.holo_red_dark)); customTextVIEw3.setText(coloredBoldSpan); }}actvity_text_vIEw.xml
<?xml version="1.0" enCoding="utf-8"?><ScrollVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:background="@androID:color/white"> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:gravity="center" androID:orIEntation="vertical" tools:context="com.harsh.customtextvIEw.CustomTextVIEwActivity"> <com.harsh.customtextvIEw.CustomTextVIEw androID:ID="@+ID/customTextVIEw1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_margintop="16dp" androID:gravity="center" androID:text="@string/roboto_regular" androID:textcolor="@color/colorPrimary" androID:textSize="18sp" app:Font="@string/Font_roboto_regular" /> <com.harsh.customtextvIEw.CustomTextVIEw androID:ID="@+ID/customTextVIEw2" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_margintop="16dp" androID:gravity="center" androID:text="@string/themed_text" androID:textcolor="@color/colorPrimary" androID:textSize="18sp" app:theme="@style/roboto_theme" /> <com.harsh.customtextvIEw.CustomTextVIEw androID:ID="@+ID/customTextVIEw3" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_margintop="16dp" androID:gravity="center" androID:text="@string/calculus_sans" androID:textcolor="@color/colorPrimary" androID:textSize="18sp" app:Font="@string/Font_calculus_sans" /> <button androID:ID="@+ID/change_to_roboto" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_margintop="16dp" androID:text="Change to Roboto" /> <button androID:ID="@+ID/change_to_calculus" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_margintop="16dp" androID:text="Change to Calculus" /> <button androID:ID="@+ID/make_text_colored" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_margintop="16dp" androID:text="Make text coloured" /> <button androID:ID="@+ID/make_specific_text_bold" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_margintop="16dp" androID:text="Make specific text bold" /> <button androID:ID="@+ID/make_text_bold_and_colored" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_margintop="16dp" androID:text="Make text bold and colored" /> </linearLayout></ScrollVIEw>
以上是内存溢出为你收集整理的android – StyleSpan不适用于TextView中的SpannableString全部内容,希望文章能够帮你解决android – StyleSpan不适用于TextView中的SpannableString所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)