Android - 设置TextView为粗体无效

33

这里是XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/modal_list_row"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="7.5dp"
    android:orientation="vertical" >

    <TextView
                android:id="@+id/title_text_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:gravity="center"
                android:text="@string/alert_title_small"
                android:textColor="@color/alert_dialog_text_color"
                android:textSize="@dimen/alert_dialog_title_font_size"
                android:textStyle="bold" />

</RelativeLayout>
虽然图形布局显示为粗体文本,但在设备上却不是。这是设备的问题还是什么原因?
更新:对于那些一直要求完整XML的人,这里进行了更新。一个简单的相对布局。这是Java代码:
this.titleTextView.setText(this.modalListState.title);

需要考虑的事情:是不是因为设备字体?有没有人有Galaxy S3来确认一下?还有可能是活动风格吗?这是整个应用程序所使用的样式:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.

-->
<style name="AppBaseTheme" parent="android:Theme.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.

    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

<style name="TransparentActivity" >
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowAnimationStyle">@android:style/Animation</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowFullscreen">false</item>
</style>

TransparentActivity样式用于相关活动。


我确定。只在代码中引用它来设置文本。 - Abdalrahman Shatou
有特定的设备吗? - DigCamara
请问您能否粘贴完整的XML代码,另外alert_title_small、alert_dialog_text_color和alert_dialog_title_font_size的值是多少?以获取更多信息。 - Nargis
@Nargis 这只是一个线性布局中的TextView。没有什么需要发布的。另外,文本颜色和大小会有影响吗? - Abdalrahman Shatou
我只需要完整的信息来检查问题。 - Nargis
显示剩余2条评论
10个回答

42

我找到了一个有趣的解决方案来解决这个问题。我在设备上使用的是自定义字体,而不是默认字体。一旦我切换到默认字体,所有的东西都按预期工作了!


1
你能告诉大家你是如何更改默认字体的吗?是通过在布局中定义typeface属性还是从三星设备设置中进行了一些操作? - skpal202
@skpal202 适用于三星设备的设置 :) - Abdalrahman Shatou
你必须更改默认的字体大小或类型吗?你可以分享任何截图吗? - skpal202
1
您可以像这样指定字体:<item name="android:fontFamily">sans-serif-light</item>。如果您删除此行,则可以设置样式。 - Cameron Lowell Palmer
1
哇,你真是节省了我很多时间。我在三星S5上遇到了这个问题! - Udayaditya Barua
显示剩余2条评论

23

有一种方法,你可以调用setTypeface方法来设置TextView的字体...

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

还可以参考这个链接...

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


1
还是不行...似乎有些东西覆盖了这个样式,尽管我确定我没有在代码中这样做。 - Abdalrahman Shatou
我之前已经在使用这个方法,但是我将当前按钮的字体作为第一个参数传递,并且遇到了和 OP 一样的问题。将其更改为 null 解决了我的问题。 - wainy
谢谢,将null(而不是TextView的字体)作为第一个参数传递也解决了我的问题。 - Michael Peterson

10

TextView在手机设备上运行时未变为粗体的原因是您在活动中使用了Calligraphy。因此,要将样式更改为粗体,您只需在java文件中编写以下代码。

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

祝你好运


这对我来说也是完全一致的 - 在注释掉 CalligraphyConfig.initDefault(..) 的调用后,xml 属性 android:textStyle="bold" 就可以正常工作了。很好的洞察力,谢谢。 - Gene Bo

7

大部分答案都是正确的。

你也可以使用所谓的:SpannableString

你可以这样使用:

String bold = "yes !";
String notBold = "no ";
SpannableString text = new SpannableString ( notBold + bold );
text.setSpan ( new StyleSpan ( Typeface.BOLD ) , notBold.length () , text .length () , 0 );
myTextView.setText ( text , BufferType.SPANNABLE );
关于 SpannableString 的好处在于您可以应用多个span,每个span可以应用在字符串的不同部分!正如您所注意到的那样,我仅在字符串的一部分上应用了粗体字体(您指定开始和结束索引),它应该看起来像这样:

no yes!

在方法setSpan中,您按照特定顺序指定要应用的SPAN、起始索引、结束索引和标志(我总是使用0)。 您甚至可以应用其他span,例如更改文本大小(使用RelativeSizeSpan ),或者甚至是颜色(使用ForegroundColorSpan ),等等! 以下是一个颜色span的示例,您可以以以下方式实现:
text.setSpan ( new ForegroundColorSpan ( Color.RED) , 0 , notBold .length () , 0 );

现在,字符串的第一部分(包含单词no)将以红色显示!


我知道这个解决方案,但是我不想每次想要将TextView设置为粗体时都使用代码! - Abdalrahman Shatou

4
我曾经遇到过一个类似的问题,与转义字符<有关。你需要使用&lt;代替它。例如:
<string name="greetings">&lt;b>From Russia with love&lt;/b></string>

官方网站上已经写得很清楚了,详情请参考这里


3
在运行过程中,可能没有使用尺寸文件中定义的字体大小。猜测您没有在模拟器中运行应用程序。
请检查以下内容以确保不是设备问题:
- 确保为目标设备度量衡正确定义了正确的尺寸文件中的字体大小。 - 一些设备允许更改默认文本大小。在设备中的“设置”下检查默认文本大小。 - 在显示布局的活动的清单文件中应用的任何样式或主题。尝试暂时删除样式/主题。 - 尝试将字体大小硬编码为偶数,例如24sp。 - 检查任何父视图/布局的缩放属性。 - 没有代码在运行时尝试更改字体样式。

2
字体大小与文本样式有什么关系?无论大小,粗体就是粗体! - Abdalrahman Shatou
这是什么字体?由于某些原因,有些字体可能没有粗体变体可用。 - skpal202

2
尝试在你的strings.xml文件中使用HTML标签。以下是实现方法,参考链接: android string.xml reading html tags problem 如果想要通过编程方式实现,请按照以下步骤进行:
TextView t = new TextView(mContext);
t.setText(Html.fromHtml("<b>This is bold</b>"));

2
我使用了这个解决方案,但除了需要编码外,它并没有起作用。 - Abdalrahman Shatou

1

请您能否将所有Java和XML代码发布,以便我们收集更多关于您的问题的信息?

可能问题不在您展示给我们的TextView上,但到目前为止

android:textStyle="bold"

似乎在您的图形布局上起作用。

@A.Shatou,请问你在Java代码中的this.modalListState.title的值是多少?由于你正在将TextView设置为它,所以能否请你发布一下那段代码? - Gaurav

1

字体必须有加粗/斜体/下划线(即目标)版本。系统不会自动为您创建它。

因此,请确保包含字体的目标变体。


我只有在assets文件夹中的blogger_sans_light字体,调用textView.setTypeface(textView.getTypeface(), Typeface.BOLD);神奇地将字体变成了粗体格式。 - Ezequiel Adrian

0
创建适用于所有设备布局的自定义主题,并将其设置到您的TextView中。
我认为问题在于文本会非常缓慢,因此您可能无法获得任何效果。
如果您已经测试了这个问题,请将字体大小增加到40sp并运行,看看会发生什么?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接