将文本视图的字体设置为 Typeface.NORMAL 没有任何效果,为什么?

set typeface of text view to Typeface.NORMAL doesn't have any effect, why?

我有 ExpandableListView 组的收听者 collapsed and expanded:

// There is a text view on group layout, no problem there.
    @Override
    public void onGroupCollapse(int groupPosition) {
        // this callback is triggered, however, once the textView is BOLD by onGroupExpanded, the textView is not set back to normal, seems this line of code does nothing...WHY?
        textView.setTypeface(textView.getTypeface(), Typeface.NORMAL);
    }

    @Override
    public void onGroupExpand(int groupPosition) {
        // it works fine
        textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
    }

正如你在上面看到的,我在组布局中有一个textView,当组展开时,我加粗 textView,如果折叠,我尝试通过 Typeface.NORMAL.

将其设置回 unbold

两个回调都被正确触发,但是,一旦 textViewonGroupExpanded(...) 回调 BOLD,之后触发 onGroupCollapse(...) 时,textView 不会设置回 NORMAL。似乎 onGroupCollapsed(...) 中的代码行什么也没做...为什么?

(再次触发 onGroupCollapse(...)。没问题!)

改用这个:

textView.setTypeface(null, Typeface.NORMAL);

您需要输入 null 以删除其他字体(在您的情况下为粗体)。我如何理解一个文本视图可以有多种字体,例如斜体+粗体等,第一个参数是指示您是否要保留这些字体。所以基本上你之前的代码保留了 BOLD 并添加了 NORMAL,并且 BOLD 获胜;-)

另请参阅此处的相关答案和评论

如果你在这个 TextView 上使用 android:fontFamily 那么我很确定

textView.setTypeface(null, Typeface.NORMAL);

将更改为默认字体。另一种方法是:

textView.setTypeface(Typeface.create(textView.getTypeface(), Typeface.NORMAL));

最初的问题是,如果样式为 NORMAL,TextView 的 setTypeface 样式会奇怪地跳过一些内容。我已经在一个版本上对此进行了测试,并且有效。