代码之家  ›  专栏  ›  技术社区  ›  Manoj Fegde

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

  •  4
  • Manoj Fegde  · 技术社区  · 11 年前

    我正在开发一个应用程序。我正在使用自定义字体。“.ttf”文件以自定义文本视图的字体。我将代码用作:

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

    现在的需求是: 我想像上面所做的那样自定义文本,以及 粗体 在.java文件中。

    请建议如何做到这一点。还有什么其他风格或自定义可以在字体上完成,请提出建议。

    5 回复  |  直到 5 年前
        1
  •  15
  •   Simon Dorociak    11 年前

    你可以通过

    textview.setTypeface(tfArchitectsDaughter, Typeface.BOLD);
    

    注: 当然你也可以使用 ITALIC BOLD_ITALIC

        2
  •  12
  •   Raghunandan    11 年前

    使用可扩展字符串。 也可以看看这个教程。 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);
    

    您也可以在文本视图中为文本使用不同的颜色。

    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);
    

    http://www.chrisumbel.com/article/android_textview_rich_text_spannablestring 。该链接为您提供了更多使用可展弦进行造型的示例。

        3
  •  5
  •   Czechnology    11 年前

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

    创建一个单独的文件作为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>
    

    在您的java文件中,如果您希望在can操作意味着之后文本为粗体

    Typeface tfArchitectsDaughter = Typeface.createFromAsset(getAssets(), "fonts/ArchitectsDaughter.ttf");
    textview.setTypeface(tfArchitectsDaughter);
    
    myTextView.setTextAppearance(getApplicationContext(), R.style.boldText);
    
        4
  •  0
  •   Manoj Fegde    11 年前

    使用代码将文本加粗为:

    TextView.setTextAppearance(getApplicationContext(), R.style.boldText);
    
        5
  •  0
  •   Optim India    11 年前

    您可以将文本加粗为:

    TextView.setTextAppearance(getApplicationContext(), R.style.boldText);