首页 文章

设置TextView样式(粗体或斜体)

提问于
浏览
711

如何在Java中设置 TextView 样式(粗体斜体)而不使用XML布局?

换句话说,我需要用Java编写 android:textStyle .

21 回答

  • 1635

    就我而言:

    1 - 设置文本

    2 - 设置字体

    holder.title.setText(item.nome);
    holder.title.setTypeface(null, Typeface.BOLD);
    
  • 4
    textView.setTypeface(null, Typeface.BOLD_ITALIC);
    textView.setTypeface(null, Typeface.BOLD);
    textView.setTypeface(null, Typeface.ITALIC);
    textView.setTypeface(null, Typeface.NORMAL);
    

    保留以前的字体

    textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC)
    
  • 121

    尝试将此设置为 TextView 为粗体或斜体

    textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
    textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);
    textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);
    
  • 30

    以编程方式:

    您可以使用 setTypeface() 以编程方式执行:

    textView.setTypeface(null, Typeface.NORMAL);      // for Normal Text
    textView.setTypeface(null, Typeface.BOLD);        // for Bold only
    textView.setTypeface(null, Typeface.ITALIC);      // for Italic
    textView.setTypeface(null, Typeface.BOLD_ITALIC); // for Bold and Italic
    

    XML:

    您可以在 <TextView /> 中直接在XML文件中设置:

    android:textStyle="normal"
    android:textStyle="normal|bold"
    android:textStyle="normal|italic"
    android:textStyle="bold"
    android:textStyle="bold|italic"
    
  • 1

    您有两种选择:

    Option 1 (仅适用于粗体,斜体和下划线):

    String s = "<b>Bolded text</b>, <i>italic text</i>, even <u>underlined</u>!"
    TextView tv = (TextView)findViewById(R.id.THE_TEXTVIEW_ID);
    tv.setText(Html.fromHtml(s));
    

    Option 2:

    使用Spannable;它更复杂,但您可以动态修改文本属性(不仅是粗体/斜体,还有颜色) .

  • 1

    以编程方式:

    您可以使用 setTypeface() 方法以编程方式执行:

    下面是默认字体的代码

    textView.setTypeface(null, Typeface.NORMAL);      // for Normal Text
    textView.setTypeface(null, Typeface.BOLD);        // for Bold only
    textView.setTypeface(null, Typeface.ITALIC);      // for Italic
    textView.setTypeface(null, Typeface.BOLD_ITALIC); // for Bold and Italic
    

    如果要设置自定义字体:

    textView.setTypeface(textView.getTypeface(), Typeface.NORMAL);      // for Normal Text
    textView.setTypeface(textView.getTypeface(), Typeface.BOLD);        // for Bold only
    textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);      // for Italic
    textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC); // for Bold and Italic
    

    XML:

    您可以直接在 <TextView /> 中的XML文件中设置,如下所示:

    android:textStyle="normal"
    android:textStyle="normal|bold"
    android:textStyle="normal|italic"
    android:textStyle="bold"
    android:textStyle="bold|italic"
    

    或者您可以设置您的fav字体(来自资产) . 了解更多信息see link

  • 1
    TextView text = (TextView)findViewById(R.id.THE_TEXTVIEW_ID);
    

    现在设置 textview 属性..

    text.setTypeface(null, Typeface.BOLD);  //-- for only bold the text
    text.setTypeface(null, Typeface.BOLD_ITALIC);  //-- for  bold & italic the text
    text.setTypeface(null, Typeface.ITALIC);  // -- for  italic the text
    
  • 2

    简单地说,如果你想制作文字 bold . 在文本视图属性的布局中写下此行

    android:textStyle="bold"
    
  • 5

    尝试通过java代码设置 TextView 样式

    txt1.setTypeface(null,Typeface.BOLD_ITALIC);
    
  • 2
    TextView text = (TextView)findViewById(R.layout.textName);
    text.setTypeface(null,Typeface.BOLD);
    
  • 80

    这将是

    yourTextView.setTypeface(null,Typeface.DEFAULT_BOLD);
    

    和斜体应该能够与 Typeface.DEFAULT_ITALC 替换 Typeface.DEFAULT_BOLD .

    让我知道它是如何工作的 .

  • 12

    使用 textView.setTypeface(Typeface tf, int style); 设置TextView的样式属性 . 有关详细信息,请参阅developer documentation .

  • 11

    试试这个:

    TextView textview = (TextView)findViewById(R.id.textview_idname);
    textview.setTypeface(null,Typeface.BOLD);
    
  • 9

    执行此操作的标准方法是使用自定义样式 . EX-

    styles.xml 中添加以下内容 .

    <resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="MyApp.TextAppearance.LoginText">
        <item name="android:textStyle">bold|italic</item>
    </style>
    

    将此样式应用于 TextView ,如下所示 .

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/MyApp.TextAppearance.LoginText" />
    
  • 6

    你可以做的一种方法是:

    myTextView.setTypeface(null, Typeface.ITALIC);
    myTextView.setTypeface(null, Typeface.BOLD_ITALIC);
    myTextView.setTypeface(null, Typeface.BOLD);
    myTextView.setTypeface(null, Typeface.NORMAL);
    

    另一个选择,如果你想保留以前的字体,并且不想丢失先前应用的那么:

    myTextView.setTypeface(textView.getTypeface(), Typeface.NORMAL);      
    myTextView.setTypeface(textView.getTypeface(), Typeface.BOLD);        
    myTextView.setTypeface(textView.getTypeface(), Typeface.ITALIC);      
    myTextView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);
    
  • 3

    你可以尝试这样:

    <string name="title"><u><b><i>Your Text</i></b></u></string>
    
  • 0

    由于我想使用自定义字体,只有几个答案的连接对我有用 . 显然我的 layout.xml 中的设置如 android:textStlyle="italic" 被AOS忽略了 . 所以最后我必须做如下:在 strings.xml 中,目标字符串被声明为:

    <string name="txt_sign"><i>The information blah blah ...</i></string>
    

    然后另外在代码中:

    TextView textSign = (TextView) findViewById(R.id.txt_sign);
    FontHelper.setSomeCustomFont(textSign);
    textSign.setTypeface(textSign.getTypeface(), Typeface.ITALIC);
    

    我没有尝试 Spannable 选项(我认为必须工作)但是

    textSign.setText(Html.fromHtml(getString(R.string.txt_sign)))
    

    没有效果 . 此外,如果我从 strings.xml 删除 italic tag ,只留下 setTypeface() 它也没有任何效果 . 棘手的Android ...

  • 0

    并且如Android Developers String Resources所述,如果您需要在样式化文本资源中使用参数,则必须转义左括号

    <resources>
    <string name="welcome_messages">Hello, %1$s! You have &lt;b>%2$d new messages&lt;/b>.</string>
    </resources>
    

    并调用formatHtml(string)

    Resources res = getResources();
    String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
    CharSequence styledText = Html.fromHtml(text);
    
  • 248

    最好的方法是在 styles.xml 中定义它

    <style name="common_txt_style_heading" parent="android:style/Widget.TextView">
            <item name="android:textSize">@dimen/common_txtsize_heading</item>
            <item name="android:textColor">@color/color_black</item>
            <item name="android:textStyle">bold|italic</item>
    </style>
    

    并在 TextView 更新它

    <TextView
         android:id="@+id/txt_userprofile"
         style="@style/common_txt_style_heading"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_centerHorizontal="true"
         android:layout_marginTop="@dimen/margin_small"
         android:text="@string/some_heading" />
    
  • 8
    AppCompatTextView text =(AppCompatTextView)findViewById(R.layout.appCompatTextView1);
    text.setTypeface(null,Typeface.BOLD);
    

    使用上述方法以编程方式设置字体 .

  • 0

    这是配置OnePlus Slate™字体的OnePlus 5T上唯一有用的东西:

    textView.setTypeface(Typeface.create(textView.getTypeface(), useBold ? Typeface.BOLD : Typeface.NORMAL));
    

    其他方法会使它在BOLD或NORMAL时回退到Roboto .

相关问题