【问题标题】:Display text in BOLD以粗体显示文本
【发布时间】:2013-02-10 07:50:22
【问题描述】:

如何在 xml 的图形布局中以粗体显示文本?我希望我的标题为粗体,而结果为正常格式。谢谢你。我希望你能帮我解决这个问题。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/coordinates"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/instruction" />

    <TextView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/result" />

    <Button
        android:id="@+id/btn_process"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn_text" />

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="300dp"
            android:orientation="vertical" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Original" />

            <ImageView
                android:id="@+id/imageSource"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/test_ideal_graph" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Result" />

            <FrameLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <ImageView
                    android:id="@+id/imageAfter"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <ProgressBar
                    android:id="@+id/progressBar"
                    style="?android:attr/progressBarStyleLarge"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </FrameLayout>
        </LinearLayout>
    </ScrollView>

</LinearLayout>

【问题讨论】:

标签: android xml layout


【解决方案1】:

strings.xml

<string name="your_string_name"><b> I am bold text</b> And I am not bold</string>

使用这种方法可以更灵活地设置文本格式。

还记得用Html.fromHtml("your html string");在TextView中设置文字

例如

String s="<b> I am text in bold </b> and I am not.";
TextView t=new TextView();
t.setText(Html.fromHtml(s));

【讨论】:

    【解决方案2】:

    例如:

    android:textStyle="bold"
    

    或者当需要斜体时:

    android:textStyle="italic"
    

    更多信息:

    http://developer.android.com/reference/android/text/style/package-summary.html

    还可以查看此线程以获取更多信息:

    How do you change text to bold in Android?

    【讨论】:

    • 如果 textView 的所有内容都不是粗体怎么办?
    • 使用,将需要加粗的文字放在标签之间
    【解决方案3】:

    如果您希望TextView 中的所有文本都使用粗体,

    android:textStyle="bold"

    如果您只想将其中的一部分加粗,您可以使用 Html.fromHtml() 从 Html 生成 Spannable,如下所示:

    String text = "This is some <b>sample</b> text"; yourTextView.setText(Html.fromHtml(text));

    这里的“样本”用粗体显示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-01
      • 1970-01-01
      • 2022-01-20
      • 2016-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-20
      相关资源
      最近更新 更多