android - 如何在 TextView 中将文本居中并将部分文本设为粗体

标签 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,但是我需要将框内的文本居中,并且我需要将名称“Connor”加粗。我如何在 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>

最佳答案

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

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

感谢大佬们的帮助!

关于android - 如何在 TextView 中将文本居中并将部分文本设为粗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4961119/

相关文章:

android - ReplacementSpan 在 Marshmallow 中不起作用

android - 更新小部件中的 textView

java - 从非 UI 线程更新 UI 方法中的状态

java - Android Studio,如何动态更改对话框中的 View ,案例 : Retrofit onSuccess

android - 谷歌地图 API 和 geocoder.getFromLocationName

android - 在 ImageView 中加载的图像隐藏了按钮

java - 警报对话框不显示

Android Textview Spannable 忽略换行符和尾随空格

java - 为什么我的 Android 应用程序无法从 JSON url 获取数据?

android - 如何读取 .txt 并将其显示为 Android 中的 TextView?