【问题标题】:How to set TextView textStyle such as bold, italic如何设置TextView的textStyle如粗体、斜体
【发布时间】:2012-12-06 15:48:38
【问题描述】:

如何在不使用 XML 布局的情况下在 Java 中设置TextView 样式(粗体或斜体)?

也就是说,我需要用Java写android:textStyle

【问题讨论】:

    标签: android textview styles


    【解决方案1】:

    你有两个选择:

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

    选项 2:

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

    【讨论】:

    • 使用typeFace,您可以为整个文本设置单一样式。
    • 当我在我的自定义行中尝试它没有得到为什么? String s1 = "你在:"; holder.address = (TextView) convertView.findViewById(R.id.address_text_view); holder.address.setText(Html.fromHtml(s1)+ track.getAddress());
    • 这个方法是很好的局部文本样式。就像大文本视图中的引用一样。
    • 第一种方法在我的情况下不起作用 private void createTextView(String title, String text) { textView = new TextView(this); textView.setTextSize(17); textView.setText(Html.fromHtml("" + title + "") + " : " + text); }
    • 如果文本因布局重新计算而变大,则 spannable 和 fromHTML 选项可能会减慢输入 / setText 的速度。如果您有其他方法可用,请避免使用它。
    【解决方案2】:

    应该是

    yourTextView.setTypeface(null,Typeface.DEFAULT_BOLD);
    

    斜体应该可以用Typeface.DEFAULT_ITALC替换Typeface.DEFAULT_BOLD

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

    【讨论】:

      【解决方案3】:

      使用textView.setTypeface(Typeface tf, int style); 设置TextView 的样式属性。请参阅developer documentation 了解更多信息。

      【讨论】:

        【解决方案4】:

        尝试在TextView 上设置粗体或斜体

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

        【讨论】:

        • 顺便说一下,如果你想清除现有的字体样式,你需要做一些不同的事情,例如:tv.setTypeface(Typeface.create(tv.getTypeface(), Typeface.NORMAL));
        • @greg7gkb tv.setTypeface(null, Typeface.BOLD); 不会这样做吗(清除现有的字体样式)?
        • 将 null 传递给 setTypeface() 意味着 TextView 使用硬编码的默认值,该默认值可能与之前设置的 Typeface 不同。
        【解决方案5】:

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

        txt1.setTypeface(null,Typeface.BOLD_ITALIC);
        

        【讨论】:

          【解决方案6】:
          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)
          

          【讨论】:

          • 去掉样式可以使用Typeface.NORMAL
          • 如果你这样做,你会丢失任何以前的字体。要保留前一个,请执行类似 textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);
          • textView.setTypeface(textView.getTypeface(), Typeface.NORMAL); 不会从TextView 中删除粗体或斜体样式。为此,您需要使用 textView.setTypeface(null, Typeface.NORMAL);
          • 要保留任何以前的字体但要摆脱粗体或斜体样式,请使用以下代码:textView.setTypeface(Typeface.create(textView.getTypeface(), Typeface.NORMAL), Typeface.NORMAL);
          • 感谢@Shnkc,您为我指明了正确的方向。其实你只需要: textView.setTypeface(Typeface.create(textView.getTypeface(), Typeface.NORMAL));
          【解决方案7】:
          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
          

          【讨论】:

            【解决方案8】:
            TextView text = (TextView)findViewById(R.layout.textName);
            text.setTypeface(null,Typeface.BOLD);
            

            【讨论】:

              【解决方案9】:

              以编程方式:

              您可以使用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:

              你可以直接在&lt;TextView /&gt;的XML文件中设置,比如:

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

              【讨论】:

              • 提问者询问如何不使用 XML 布局。
              • 检查问题with in Java and without using XML 顺便说一句,它也会对其他人有所帮助。
              • 是的。我是通过谷歌来到这里的,它对我有帮助。谢谢:)
              【解决方案10】:

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

              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" />
              

              【讨论】:

                【解决方案11】:

                因为我想使用自定义字体,所以只有几个答案的组合对我有用。显然,我的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(),它也没有任何效果。棘手的安卓...

                【讨论】:

                  【解决方案12】:

                  试试这个:

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

                  【讨论】:

                    【解决方案13】:

                    以编程方式:

                    您可以使用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:

                    你可以像这样直接在&lt;TextView /&gt;的XML文件中设置:

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

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

                    【讨论】:

                      【解决方案14】:

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

                      【讨论】:

                        【解决方案15】:

                        最好的方法是在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" />
                        

                        【讨论】:

                          【解决方案16】:

                          如果你想让文本粗体。 在文本视图属性的布局中写下这一行

                          android:textStyle="bold"
                          

                          【讨论】:

                            【解决方案17】:
                            AppCompatTextView text =(AppCompatTextView)findViewById(R.layout.appCompatTextView1);
                            text.setTypeface(null,Typeface.BOLD);
                            

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

                            【讨论】:

                              【解决方案18】:

                              您可以做的一种方法是:

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

                              【讨论】:

                                【解决方案19】:

                                你可以这样试试:

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

                                【讨论】:

                                  【解决方案20】:

                                  就我而言:

                                  1 - 设置文本

                                  2 - 设置字体

                                  holder.title.setText(item.nome);
                                  holder.title.setTypeface(null, Typeface.BOLD);
                                  

                                  【讨论】:

                                    【解决方案21】:

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

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

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

                                    【讨论】:

                                      【解决方案22】:
                                      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)
                                      

                                      【讨论】:

                                        【解决方案23】:

                                        试试这个:

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

                                        【讨论】:

                                          【解决方案24】:

                                          根据样式选择标准,您可以做的最简单的方法是:

                                          String pre = "", post = "";
                                          
                                          if(isBold){
                                              pre += "<b>"; post += "</b>";
                                          }
                                          if(isItalic){
                                              pre += "<i>"; post += "</i>";
                                          }
                                          if(isUnderline){
                                              pre += "<u>"; post += "</u>";
                                          }
                                          
                                          textView.setText(Html.fromHtml(pre + editText.getText().toString()+ post));
                                          // you can also use it with EidtText
                                          editText.setText(Html.fromHtml(pre + editText.getText().toString()+ post));
                                          

                                          【讨论】:

                                            【解决方案25】:

                                            1) 您可以使用 TypeFace 进行设置。 2)您可以直接在strings.xml中使用 (在您的值文件夹中) 3) 你可以 String myNewString = " 这是我的粗体文本 这是我的斜体字符串 这是我的下划线字符串

                                            【讨论】:

                                              【解决方案26】:

                                              在 AndroidX 中使用简化标签时,请考虑使用 HtmlCompat.fromHtml()

                                              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(HtmlCompat.fromHtml(s, FROM_HTML_MODE_LEGACY));
                                              

                                              【讨论】:

                                                【解决方案27】:

                                                您可以使用下面给出的示例设置不同的字体 -

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

                                                或者,如果您想设置不同的字体及其字体。将其添加到资产或原始文件夹,然后像使用它一样

                                                  Typeface face= Typeface.createFromAsset(getAssets(), "font/font.ttf");
                                                  tv1.setTypeface(face);
                                                
                                                  Typeface face1= Typeface.createFromAsset(getAssets(), "font/font1.ttf");
                                                  tv2.setTypeface(face1);
                                                

                                                【讨论】:

                                                  猜你喜欢
                                                  • 2012-10-29
                                                  • 1970-01-01
                                                  • 2011-06-10
                                                  • 2023-03-29
                                                  • 1970-01-01
                                                  • 2015-10-02
                                                  • 1970-01-01
                                                  • 2018-02-09
                                                  相关资源
                                                  最近更新 更多