自定义字体和粗体样式的文本

Man*_*gde 4 fonts android textview android-fonts

我正在开发一个应用程序.我正在使用自定义字体.".ttf"文件来自定义文本视图的字体.我使用的代码如下:

Typeface tfArchitectsDaughter = Typeface.createFromAsset(getAssets(), "fonts/ArchitectsDaughter.ttf");
textview.setTypeface(tfArchitectsDaughter);
Run Code Online (Sandbox Code Playgroud)

现在需要的是:我想让文本自定义,如上所述,以及.java文件中的BOLD样式.

怎么做请建议.还有什么其他样式或定制可以在字体上做,请建议.

谢谢.

Sim*_*iak 15

你可以实现它

textview.setTypeface(tfArchitectsDaughter, Typeface.BOLD);
Run Code Online (Sandbox Code Playgroud)

注意:肯定你也可以使用ITALICBOLD_ITALIC


Rag*_*dan 12

使用SpannableString.另请查看本教程.http://blog.stylingandroid.com/archives/177.

String tempString="Copyright";
TextView text=(TextView)findViewById(R.id.text);
SpannableString spanString = new SpannableString(tempString);
spanString.setSpan(new UnderlineSpan(), 0, spanString.length(), 0);
spanString.setSpan(new StyleSpan(Typeface.BOLD), 0, spanString.length(), 0);
spanString.setSpan(new StyleSpan(Typeface.ITALIC), 0, spanString.length(), 0);
text.setText(spanString);
Run Code Online (Sandbox Code Playgroud)

您还可以在textview中为文本使用不同的颜色.

SpannableString text = new SpannableString("Lorem ipsum dolor sit amet");  
// make "Lorem" (characters 0 to 5) red  
text.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0);  
textView.setText(text, BufferType.SPANNABLE);
Run Code Online (Sandbox Code Playgroud)

http://www.chrisumbel.com/article/android_textview_rich_text_spannablestring.该链接为您提供了使用spannable字符串进行样式设置的更多示例.


小智 5

您可以使用以下代码将文本设置为粗体

创建一个单独的文件作为style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="boldText">
        <item name="android:textStyle">bold|italic</item>
        <item name="android:textColor">#FFFFFF</item>
    </style>

    <style name="normalText">
        <item name="android:textStyle">normal</item>
        <item name="android:textColor">#C0C0C0</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

并且在你的java文件中如果你希望文本在行动之后变为粗体意味着

Typeface tfArchitectsDaughter = Typeface.createFromAsset(getAssets(), "fonts/ArchitectsDaughter.ttf");
textview.setTypeface(tfArchitectsDaughter);

myTextView.setTextAppearance(getApplicationContext(), R.style.boldText);
Run Code Online (Sandbox Code Playgroud)