Как отцентрировать текст в TextView и сделать часть текста жирным - PullRequest
1 голос
/ 10 февраля 2011

Прямо сейчас, у меня есть таблица кнопок изображений 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 в нижней части экрана, однако мне нужно, чтобы текст внутри поля был центрирован, и мне нужно, чтобы имя «Коннор» было выделено жирным шрифтом. Как мне сделать это в 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 ]

7 голосов
/ 10 февраля 2011

Неважно, я понял это. Я устанавливаю гравитацию textView по центру и устанавливаю текст в строку «нижний колонтитул», определенную в моем файле res / values ​​/ strings.xml как:

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

Спасибо за помощь, ребята!

0 голосов
/ 31 декабря 2018

Вы можете использовать теги HTML в вашем файле strings.xml или программно установить TextView. Для выравнивания текста по центру вы можете использовать android: textAlignment = "center" или android: gravity = "center" , но в в этом случае убедитесь, что ширина вашего textView равна match_parent , если это wrap_content и лучше использовать android: layout_gravity = "center"

0 голосов
/ 10 февраля 2011

Возможно, вы захотите использовать GridView для репликации таблицы 2x3 и для каждого элемента создать макет, содержащий текстовое представление с атрибутом android:layout_gravity="center_horizontal", и накачать этот макет для каждого вида в сетке.

взгляните на учебник GridView , чтобы получить преимущество в этом.

Взгляните здесь перед надуванием пользовательских макетов в обсуждении представления.

0 голосов
/ 10 февраля 2011

Возможно, вам лучше будет перейти на использование RelativeLayout вместо вложенных LinaerLayout s.В пределах RelativeLayout вы можете запросить, чтобы элемент был по центру по горизонтали .

0 голосов
/ 10 февраля 2011

Вы можете использовать HTML-теги <b> и <i> в строках, чтобы пометить жирным шрифтом и курсивом.Что касается центрирования, кажется, что ваш макет слишком запутан.Вам нужно а) упростить его и б) опубликовать весь макет, чтобы мы могли понять, почему все так, как есть.

0 голосов
/ 10 февраля 2011

Я думаю, что это может быть только сделано в коде .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...