ConstraintLayout 在 TextView 中右对齐文本

Ang*_*ina 3 android textview android-constraintlayout

当 TextView 位于 ConstraintLayout 内时,如何在 TextView 中对齐文本?我无法使用layout_width="match_parent. 试过了,不起作用:(

android:gravity="start"
android:textAlignment="gravity"
Run Code Online (Sandbox Code Playgroud)

Rob*_*nde 12

  1. 使 TextView 与父级匹配:

    android:layout_width="0dp"
    
    Run Code Online (Sandbox Code Playgroud)
  2. 然后定义 textAlignment 属性:

    android:textAlignment="viewEnd"
    
    Run Code Online (Sandbox Code Playgroud)

例如,在从左到右的语言中右对齐:

    <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" />
Run Code Online (Sandbox Code Playgroud)

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


Dav*_*eys 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>
Run Code Online (Sandbox Code Playgroud)

现在,如果您创建了这个布局,TextView 将位于中心。这是 ConstraintLayout 的默认行为,如果您以这种方式设置约束,它总是尝试将视图居中。此行为可能会受到TextView 属性的app:layout_constraintHorizontal_bias影响。app:layout_constraintVertical_bias两者的默认值都是 0.5,您可以将其转换为 50%。如果将它们都设置为 0,您会发现 TextView 位于左上角位置。如果将它们都设置为 1,则 TextView 将位于右下角位置。您明白了,这样您就可以将 TextView 或任何 View 放置在您想要的任何位置。

偏差属性也在此处的官方文档中进行了描述。

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>
Run Code Online (Sandbox Code Playgroud)


Mim*_*ico -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" />
Run Code Online (Sandbox Code Playgroud)