Как центрировать текст в textView в этом случае? - PullRequest
2 голосов
/ 29 января 2020

Я хочу поместить текст в textView_title и textView_subtitle в центр экрана. Могу ли я узнать, как это можно сделать? Я пытался android:layout_gravity and android:gravity, и они не работают.

Ниже приведен файл XML с макетом ограничения.

    <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="0dp"
            android:layout_height="?attr/actionBarSize"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toStartOf="@id/textView_login"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent">

            <TextView
                    android:id="@+id/textView_title"
                    android:lines="1"
                    android:ellipsize="end"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintBottom_toTopOf="@id/textView_subtitle"
                    tool:text="textView"/>

            <TextView
                    android:id="@+id/textView_subtitle"
                    android:lines="1"
                    android:ellipsize="end"
                    android:textColor="@color/foodDollarYellow"
                    android:textSize="12sp"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/textView_title"
                    app:layout_constraintBottom_toBottomOf="parent"
                    tool:text="textView"/>

        </androidx.constraintlayout.widget.ConstraintLayout>

        <TextView
                android:id="@+id/textView_login"
                android:textAlignment="textEnd"
                android:text="註冊/登入"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="16dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                tool:ignore="HardcodedText"/>

    </androidx.constraintlayout.widget.ConstraintLayout>

layout

Ответы [ 2 ]

1 голос
/ 29 января 2020

Добавьте android:textAlignment="center" к вашему textViews и сделайте родительскую ширину равной ширине экрана:

 <androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="0dp"
    android:layout_height="?attr/actionBarSize"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/textView_title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:lines="1"
            android:text="text1"
            android:textAlignment="center"
            app:layout_constraintBottom_toTopOf="@id/textView_subtitle"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView_subtitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:lines="1"
            android:text="text2"
            android:textAlignment="center"
            android:textSize="12sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/textView_title" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <TextView
        android:id="@+id/textView_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:text="註冊/登入"
        android:textAlignment="textEnd"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="HardcodedText" />



</androidx.constraintlayout.widget.ConstraintLayout>

Это будет выглядеть так:

enter image description here


Если вы хотите избежать вложения, вы можете добиться этого, используя единый макет:

   <androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <TextView
        android:id="@+id/textView_title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:lines="1"
        android:text="text1"
        android:textAlignment="center"
        app:layout_constraintBottom_toTopOf="@id/textView_subtitle"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView_subtitle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:lines="1"
        android:text="text2"
        android:textAlignment="center"
        android:textSize="12sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textView_title" />


    <TextView
        android:id="@+id/textView_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="註冊/登入"
        android:textAlignment="textEnd"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/textView_title"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints" />


</androidx.constraintlayout.widget.ConstraintLayout>

И это будет выглядеть так:

enter image description here

Вы можете достичь этого макета с вложением или без него, но более рекомендуется избегать вложений, когда возможно, если вы используете constaintLayout

Редактировать

Это новый макет: (Я добавил 1 Guildeline )

 <androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/constraintLayout"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="@+id/textView_subtitle"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="@+id/guideline2"
    app:layout_constraintTop_toTopOf="parent">


</androidx.constraintlayout.widget.ConstraintLayout>

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.7" />

<TextView
    android:id="@+id/textView_login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="註冊/登入"
    android:textAlignment="textEnd"
    app:layout_constraintBottom_toBottomOf="@+id/textView_subtitle"
    app:layout_constraintEnd_toEndOf="@+id/constraintLayout"
    app:layout_constraintStart_toStartOf="@+id/guideline2"
    app:layout_constraintTop_toTopOf="parent"
    tools:ignore="MissingConstraints" />

<TextView
    android:id="@+id/textView_subtitle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:lines="1"
    android:text="long longggggggggggggggggggggggggggggggggggggggggg"
    android:textAlignment="center"
    android:textSize="12sp"
    app:layout_constraintEnd_toStartOf="@+id/guideline2"
    app:layout_constraintStart_toStartOf="@+id/textView_title"
    app:layout_constraintTop_toBottomOf="@+id/textView_title" />

<TextView
    android:id="@+id/textView_title"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:lines="1"
    android:text="longggggggggggggggggggggggggggggggggg"
    android:textAlignment="center"
    app:layout_constraintEnd_toStartOf="@+id/guideline2"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

Вот как это будет выглядеть:

enter image description here

0 голосов
/ 29 января 2020

Решение 1: добавить в текстовое представление:

app:layout_constraintTop_toTopOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent"

отсюда:

<TextView
    android:id="@+id/textView_login"
    android:textAlignment="textEnd"
    android:text="註冊/登入"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    tools:ignore="MissingConstraints" />

Решение 2

использовать относительный макет вместо ограничения (я беру первый макет ограничения, а не второй), затем

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/relative"
 >

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/textView_login"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

        <TextView
            android:id="@+id/textView_title"
            android:lines="1"
            android:ellipsize="end"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@id/textView_subtitle"
      />

        <TextView
            android:id="@+id/textView_subtitle"
            android:lines="1"
            android:ellipsize="end"
            android:textColor="@color/foodDollarYellow"
            android:textSize="12sp"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@id/textView_title"
            app:layout_constraintBottom_toBottomOf="parent"
  />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <TextView
        android:id="@+id/textView_login"
        android:textAlignment="textEnd"
        android:text="註冊/登入"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_centerHorizontal="true"
        tools:ignore="MissingConstraints" />

</RelativeLayout>

android:layout_centerHorizontal="true" эта строка приведет ваше текстовое представление в centretop

надеюсь, мой ответ помог ...

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