ConstraintLayout在TextView中对齐文本

时间:2018-05-29 11:33:51

标签: android textview android-constraintlayout

如何在TextView内部对齐TextraintLayout中的文本?我无法使用layout_width="match_parent。试过这个,不起作用:(

android:gravity="start"
android:textAlignment="gravity"

8 个答案:

答案 0 :(得分: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 :(得分:1)

  1. 使TextView与以下项匹配为父项:

    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" />

请注意,您可以为documentation中定义的textAlignment使用不同的值。

答案 2 :(得分:0)

试试这个:

<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" />

答案 3 :(得分: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" />

答案 4 :(得分:0)

解决方案:

使用此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>

希望有帮助。

答案 5 :(得分:0)

通常,如果一行中只有一个以上TextView,则将第一个TextView左对齐并不容易。我已经为此目的使用了一个准则,它可以完美地工作。如果删除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" />

答案 6 :(得分:0)

不幸的是,我们缺少布局的整个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或任何视图放置在所需的任何位置。

偏差属性也在官方文档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>

答案 7 :(得分:-1)

使用设计因为它比xml video file

容易得多
相关问题