android - 将 TextView 的字体设置为 Typeface.NORMAL 没有任何效果,为什么?

标签 android expandablelistview android-typeface

我有 ExpandableListView 的监听器对于组折叠和展开:

// 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 上使用 android:fontFamily 那么我很确定

textView.setTypeface(null, Typeface.NORMAL);

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

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

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

关于android - 将 TextView 的字体设置为 Typeface.NORMAL 没有任何效果,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47797541/

相关文章:

android - textview 不服从资源 fontfamily

android - 当通过 Intent 调用 BroadcastReceiver 时,它在哪个进程上运行?

java - 由于错误代码 : Failed to find configured root that contains,无法使用共享图像按钮

android - 如何在 android 中创建带有节标题的可扩展 ListView

flutter - 我想在附件图像中进行如下所示的可扩展 ListView 。如何在 flutter 打中实现这种功能?

Android 字体 ttf 或 otf 字体呈现随机符号/字母

java - 使用 Assets 从android中的文件中读取

java - 在 MP3 ID3v2 标签中设置歌词

android - 覆盖默认的 expandablelistview 扩展行为

android - 在 Android 应用程序中更改字体会使应用程序崩溃 - 无法制作 native 字体