ConstraintLayout如何使TextView中的文本右对齐

5
当TextView位于ConstraintLayout中时,如何对齐文本?我不能使用layout_width="match_parent"。尝试过了,但不起作用 :(
android:gravity="start"
android:textAlignment="gravity"

发布完整的布局 XML。 - Abhay Koradiya
8个回答

15
  1. 使用以下代码将 TextView 设置为 match parent:

    android:layout_width="0dp"
    
  2. 接着定义textAlignment属性:

    android:textAlignment="viewEnd"
    
    例子,在从左往右的语言中向右对齐:
        <TextView
         android:id="@+id/tv_item"
         style="@style/text_highlight_small_title"
         android:layout_width="0dp"
         android:layout_height="wrap_content"
         android:text="@string/my_text"
         android:textAlignment="viewStart"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toTopOf="parent"
         tools:text="Sample Text" />
    

    请注意,您可以使用文档中定义的不同值作为textAlignment的值。 文档.


6

很遗憾,我们缺少您的布局的整个XML,但让我们假设您有类似这样的东西:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Something Important to align"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

现在,如果您创建了确切的此布局,TextView 将位于中心。这是 ConstraintLayout 的默认行为,如果您以这种方式设置约束条件,则它始终尝试将视图居中。TextView 的 app:layout_constraintHorizontal_biasapp:layout_constraintVertical_bias 属性可以影响此行为。两者的默认值都为 0.5,您可以将其翻译为 50%。如果您将它们都设置为 0,则会发现 TextView 位于左上角位置。如果您将它们都设置为 1,则 TextView 将位于右下角位置。您已经明白了,通过这种方式,您可以将 TextView 或任何 View 定位到任何位置。

偏置属性也在官方文档here中描述。

完全工作的示例,其中 TextView 垂直居中并水平向右对齐:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Something Important to align"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.5" />

</android.support.constraint.ConstraintLayout>

太好了,我不知道这个。 - support_ms
这应该被标记为正确答案。 - Adeel Ahmad

2

解决方法:

使用以下 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="TextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginLeft="16dp"
        android:layout_marginEnd="16dp"
        android:gravity="start"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginRight="16dp" />
</android.support.constraint.ConstraintLayout>

希望这能帮到你。

1
使用这个 XML 代码。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginLeft="16dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginRight="16dp" />
</android.support.constraint.ConstraintLayout>

1
通常情况下,如果在一行中有多个TextView,则将第一个TextView对齐到左侧并不容易。我使用了Guideline来实现这个目的,并且它完美地工作。如果您删除GuideLine,则无法正常工作。 以下是xml代码:
<TextView
    android:id="@+id/textView1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="130dp"
    android:ellipsize="end"
    android:maxLines="1"
    android:text="first very long very long long long long long very long text"
    android:textAlignment="viewStart"
    app:layout_constraintEnd_toStartOf="@id/textView2"
    app:layout_constraintStart_toStartOf="@+id/guideline"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="130dp"
    android:text="second text"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_begin="0dp" />

0

试试这个:

<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:gravity="right"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginLeft="16dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginRight="16dp" />

-1

试试这个:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.YOURUSER.YOURPROJECT.YOURACTIVITY">

    <TextView
        android:id="@+id/TextView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

-1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接