如何在TextView中居中文本并使文本的一部分变为粗体

时间:2011-02-10 18:29:29

标签: android textview

现在,我有一个2x3的图像按钮表,我需要在窗口底部加一个TextView这就是我对TextView所拥有的:

<TextView
    android:layout_height="wrap_content"
    android:layout_width="match_parent" 
    android:layout_gravity="center_horizontal|bottom"
    android:text = "Hello Connor"
    android:textColor = "#000000"
    android:background = "@drawable/back"/>     
</LinearLayout>
</LinearLayout>
</LinearLayout>

现在它在屏幕底部创建了一个TextView,但我需要框内的文字居中,我需要这个名字,&#34; Connor&#34;要大胆。我如何在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"
android:background="@color/all_white">
<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <ImageButton
                android:background = "@android:color/transparent"
                android:id="@+id/imagebutton1"
                android:src="@drawable/button"
                android:layout_width = "wrap_content"
                android:layout_height = "wrap_content"
                android:scaleType = "fitXY"
                android:layout_marginTop = "50px"
                android:layout_marginLeft = "40px"
                android:layout_marginRight = "20px"
                android:layout_marginBottom = "50px"
                android:layout_weight="1"/>
            <ImageButton
                android:background = "@android:color/transparent"
                android:id="@+id/imagebutton2"
                android:src="@drawable/button"
                android:layout_width = "wrap_content"
                android:layout_height = "wrap_content"
                android:scaleType = "fitXY"
                android:layout_marginTop = "50px"
                android:layout_marginLeft = "20px"
                android:layout_marginRight = "40px"
                android:layout_marginBottom = "50px"
                android:layout_weight="1"/>
        </LinearLayout>    
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <ImageButton
                android:background = "@android:color/transparent"
                android:id="@+id/imagebutton3"
                android:src="@drawable/button"
                android:layout_width = "wrap_content"
                android:layout_height = "wrap_content"
                android:scaleType = "fitXY"
                android:layout_marginLeft = "40px"
                android:layout_marginRight = "20px"
                android:layout_marginBottom = "50px"
                android:layout_weight="1"/>
            <ImageButton 
                android:background = "@android:color/transparent"
                android:layout_height="wrap_content"
                android:id="@+id/imagebutton4" 
                android:src="@drawable/button" 
                android:scaleType = "fitXY"
                android:layout_marginLeft = "20px"
                android:layout_marginRight = "40px"
                android:layout_marginBottom = "50px" 
                android:layout_width="wrap_content" 
                android:layout_weight="1"/>
        </LinearLayout> 
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <ImageButton 
                android:background = "@android:color/transparent"
                android:layout_height="wrap_content" 
                android:id="@+id/imagebutton5" 
                android:src="@drawable/button" 
                android:scaleType = "fitXY"
                android:layout_marginLeft = "40px"
                android:layout_marginRight = "20px"
                android:layout_marginBottom = "50px"
                android:layout_width="wrap_content"
                android:layout_weight="1"/>
            <ImageButton
                android:background = "@android:color/transparent"
                android:id="@+id/imagebutton6"
                android:src="@drawable/button"
                android:layout_width = "wrap_content"
                android:layout_height = "wrap_content"
                android:scaleType = "fitXY"
                android:layout_marginLeft = "20px"
                android:layout_marginRight = "40px"
                android:layout_marginBottom = "50px"
                android:layout_weight="1"/>
        </LinearLayout>
        <TextView
        android:layout_height="wrap_content" 
        android:layout_gravity="center_horizontal|bottom"
        android:text = "Powered by Alarm.com"
        android:textColor = "#000000"
        android:background = "@drawable/back" android:layout_width="match_parent"/>     
    </LinearLayout>
</LinearLayout>
</LinearLayout>

6 个答案:

答案 0 :(得分:7)

没关系,我明白了。我将textView的重力设置为居中,并将文本设置为我的res / values / strings.xml文件中定义的字符串“footer”:

<string name="footer">Hello <b>Connor</b></string>

感谢帮帮!

答案 1 :(得分:0)

我认为这只能是done in code

答案 2 :(得分:0)

您可以在字符串中使用HTML标记<b><i>来标记粗体和斜体。 至于居中,看起来你的布局过于复杂。您需要a)简化它,并且b)发布整个布局,以便我们可以看到为什么事情就像它们一样。

答案 3 :(得分:0)

切换到使用RelativeLayout代替嵌套LinaerLayout可能会更好。在RelativeLayout范围内,您可以请求某个项目为centered horizontally

答案 4 :(得分:0)

您可能需要考虑使用GridView复制2x3表,并为每个元素创建一个包含带有android:layout_gravity="center_horizontal"属性的TextView的布局,并为网格中的每个View扩展此布局。

看一下GridView tutorial以了解这一点。

在视图讨论中查看here预先夸大自定义布局。

答案 5 :(得分:0)

您可以在strings.xml文件中使用HTML标记,也可以通过编程将其设置为TextView。对于居中对齐,您可以使用 android:textAlignment =“ center” android:gravity =“ center” ,但在这种情况下,请确保textView的宽度为 match_parent ,如果为 wrap_content ,则最好使用 android:layout_gravity =“ center”

相关问题