【问题标题】:setting TextView typeface to bold and serif separately resets each other将 TextView 字体设置为粗体和衬线分别重置彼此
【发布时间】:2016-12-29 14:14:54
【问题描述】:

我的应用中有一个文本视图和两个微调器。一个微调器选择字体(默认/衬线/等宽)。另一个微调器选择字体样式(Normal/Bold/Italic/Bold Italic)。

这是我的代码:

tvQuoteTextSample = (TextView)findViewById(R.id.preview_QuoteText);

从字体微调器设置字体:

switch(selectedTypeFace)
                    {
                        case "Serif":
                            tvQuoteTextSample.setTypeface(Typeface.SERIF);
                            break;
                        case "Monospace":
                            tvQuoteTextSample.setTypeface(Typeface.MONOSPACE);
                            break;
                        default:
                            tvQuoteTextSample.setTypeface(Typeface.DEFAULT);
                            break;
                    }

从字体样式微调器设置字体样式:

switch (fontStyle) {
                        case "Bold":
                            tvQuoteTextSample.setTypeface(null, Typeface.BOLD);
                            break;
                        case "Italic":
                            tvQuoteTextSample.setTypeface(null, Typeface.ITALIC);
                            break;
                        case "Bold Italic":
                            tvQuoteTextSample.setTypeface(null, Typeface.BOLD_ITALIC);
                            break;
                        default:
                            tvQuoteTextSample.setTypeface(null, Typeface.NORMAL);
                            break;
                    }

当我应用 fontStyle 微调器时,应用的字体重置为默认值。同样,当我应用 TypeFace 时,fontStyle 会重置为 Normal。

我希望两个微调器一起工作。即一个微调器的输出将应用于 textView 而不会重置以前的样式。如何做到这一点?

【问题讨论】:

标签: android


【解决方案1】:

您似乎在更改样式时通过提供null 参数来重置TypeFace

相反,保留当前的TypeFace 并且只更改样式:

tvQuoteTextSample.setTypeface(tvQuoteTextSample.getTypeFace(), Typeface.BOLD);

【讨论】:

  • 它可以工作,但是当我先设置字体样式然后设置字体时,字体样式会再次重置。我不能使用:tvQuoteTextSample.setTypeface(tvQuoteTextSample.getTypeFace(), Typeface.SERIF);
  • 没关系,我修好了。我使用了一个 int 变量 FONT_STYLE 在其中存储 Typeface.BOLD,然后使用该变量来: tvQuoteTextSample.setTypeface(Typeface.SERIF, FONT_STYLE);
猜你喜欢
  • 2015-03-11
  • 2023-03-29
  • 2020-01-22
  • 2011-03-30
  • 1970-01-01
  • 2013-06-04
  • 2015-09-23
  • 2021-12-31
  • 1970-01-01
相关资源
最近更新 更多