Android - 将TextView设置为粗体不起作用

Abd*_*tou 26 android textview

这是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>
Run Code Online (Sandbox Code Playgroud)

尽管图形布局显示粗体文本,但设备上却没有.它是设备还是什么?

更新:

对于那些不断要求完整XML的人来说,这里会更新.一个简单的相对布局.这是Java代码:

this.titleTextView.setText(this.modalListState.title);
Run Code Online (Sandbox Code Playgroud)

需要考虑的事项:可能是因为设备字体?有没有人有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>
Run Code Online (Sandbox Code Playgroud)

TransparentActivity样式用于相关活动.

Abd*_*tou 36

好吧,我发现这个问题很愚蠢.我在设备上使用自定义字体,而不是默认字体.一旦我切换到默认值,每件事都按预期工作!


Nil*_*tel 20

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

 textView.setTypeface(null, Typeface.BOLD_ITALIC);
 textView.setTypeface(null, Typeface.BOLD);
 textView.setTypeface(null, Typeface.ITALIC);
Run Code Online (Sandbox Code Playgroud)

并参考此链接......

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


Sid*_*eja 7

在移动设备上运行应用程序时,TextView不会变为粗体的原因是,您必须在活动中使用书法。因此,要将样式更改为粗体,您要做的就是在Java文件中编写以下代码。

 textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
Run Code Online (Sandbox Code Playgroud)

祝好运


Lee*_*elo 5

大多数答案都是正确的.

您还可以使用所谓的: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 );
Run Code Online (Sandbox Code Playgroud)

是什么样的好的SpannableString是可以应用在字符串的不同部分复式跨度,每!正如您所注意到的,我只在字符串的一部分上应用了粗体类型的面(您指定了开始和结束索引),它应该如下所示:

不是的!

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

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

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

text.setSpan ( new ForegroundColorSpan ( Color.RED) , 0 , notBold .length () , 0 );
Run Code Online (Sandbox Code Playgroud)

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