以粗体显示文字

时间:2013-02-25 07:56:13

标签: android xml layout

如何在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>

3 个答案:

答案 0 :(得分:4)

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中设置文本

<强> e.g。

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

答案 1 :(得分:2)

如果您希望TextView中的所有文字都以粗体显示,

android:textStyle="bold"

如果您只希望其中的一部分为粗体,则可以使用Spannable从Html生成Html.fromHtml(),如下所示:

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

此处“样本”将以粗体显示。

答案 2 :(得分:1)

例如:

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?