32

Here is the 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>

Although the Graphical Layout shows bold text, on the device it's not. Is it a device thing or what?

Update:

For people who keeps on asking for the ful XML, here it is updated. A simple relative layout. And here is the Java Code:

this.titleTextView.setText(this.modalListState.title);

Things to think about: Could it be due to the device typefaces? Does anyone have Galaxy S3 to confirm? Could it be the activity style? Here is the one used for the whole app:

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

The TransparentActivity style is used for the concerned activity.

4

10 回答 10

40

好吧,我为这个问题找到了一个愚蠢的答案。我在设备上使用了自定义字体,而不是默认字体。一旦我切换到默认的,一切都按预期工作!

于 2013-06-12T10:47:01.137 回答
23

有一种方法,您可以通过调用 setTypeface 方法来设置 textview 字体...

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

并参考此链接...

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

于 2013-06-12T05:37:44.757 回答
10

当您在移动设备上运行应用程序时,TextView 没有变为粗体的原因是您必须在活动中使用了 Calligraphy。因此,要将样式更改为粗体,您只需在 java 文件中编写以下代码。

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

祝你好运

于 2018-04-13T07:46:57.920 回答
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的好处在于您可以应用多个跨度,每个跨度都在字符串的不同部分!正如您所注意到的,我仅在字符串的一部分上应用了粗体字面(您指定了开始和结束索引),它应该如下所示:

,是的!

在方法setSpan中,您按特定顺序指定要应用的 SPAN、起始索引、结束索引和标志(我总是使用 0)。

您甚至可以应用其他跨度,例如更改文本大小(使用RelativeSizeSpan),甚至颜色(使用ForegroundColorSpan)等等!

这是颜色范围的示例,您可以通过以下方式实现:

text.setSpan ( new ForegroundColorSpan ( Color.RED) , 0 , notBold .length () , 0 );

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

于 2013-06-12T06:42:38.417 回答
3

在运行期间可能未选取尺寸文件中定义的字体大小。猜猜你没有在模拟器中运行应用程序。

检查以下内容以确保其不是设备。

  • 确保为目标设备指标的正确尺寸文件正确定义字体大小
  • 某些设备允许更改默认文本大小。检查设备中“设置”下的默认文本大小。
  • 在清单文件中为显示布局的 Activity 应用的任何样式或主题。尝试删除样式/主题一段时间。
  • 尝试将字体大小硬编码为偶数,例如 24sp
  • 检查任何父视图/布局的比例属性。
  • 没有代码在运行时尝试更改字体样式。
于 2013-06-07T23:06:33.160 回答
3

我遇到了与转义字符类似的问题<。你必须&lt;改用。例如:

<string name="greetings">&lt;b>From Russia with love&lt;/b></string>

官方网站上写的。

于 2017-03-16T10:12:48.767 回答
2

尝试使用 HTML 标签在您的 strings.xml 文件中。这是你可以做到的,android string.xml读取html标签问题

要以编程方式进行,

TextView t = new TextView(mContext);
t.setText(Html.fromHtml("<b>This is bold</b>"));
于 2013-06-12T05:13:57.903 回答
1

您能否发布您所有的 java 和 xml 代码,以便我们收集有关您的问题的更多信息?

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

android:textStyle="bold"

似乎适用于您的图形布局。

于 2013-06-12T04:59:48.537 回答
1

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

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

于 2016-01-01T09:58:12.760 回答
0

为或所有设备布局创建自定义主题。并设置到您的textview中。

我认为问题是文本会很慢,所以你可以获得任何效果。

如果您已经测试过这个问题,那么将字体大小更改为 40sp 并运行会发生什么?

于 2013-06-12T04:51:49.033 回答