简体   繁体   English

ConstraintLayout在TextView中对齐文本

[英]ConstraintLayout align text right in TextView

How to align text in TextView when it is inside ConstraintLayout? 当文本在ConstraintLayout中时,如何在TextView中对齐文本? I cannot use layout_width="match_parent . Tried this, does not work :( 我不能使用layout_width="match_parent 。尝试了一下,不起作用:(

android:gravity="start"
android:textAlignment="gravity"
  1. Make the TextView match parent with: 使TextView与以下项成为父项:

     android:layout_width="0dp" 
  2. Then define the textAlignment property: 然后定义textAlignment属性:

     android:textAlignment="viewEnd" 

Example, align right in a left to right language: 例如,以左到右语言右对齐:

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

Note you can use different values for textAlignment as defined in the documentation . 请注意,可以为文档中定义的textAlignment使用不同的值。

use this xml code 使用此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>

Try this: 尝试这个:

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

Try this: 尝试这个:

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

Solution: 解:

use this xml code 使用此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>

Hope it helps. 希望能帮助到你。

In general, if you have more then one TextView in a single line it is not trivial to align the first TextView to the left. 通常,如果一行中只有一个TextView,则将第一个TextView左对齐并不容易。 I have used a Guideline for this pourpose and it works perfectly. 我已经为此目的使用了一个准则,它可以完美地工作。 If you remove the GuideLine it doesn't works. 如果删除GuideLine,则无法使用。 Here is the xml: 这是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" />

Unfortunately, we are missing the whole XML of your layout, but let's assume you have something like this: 不幸的是,我们丢失了布局的整个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>

Now if you create exactly this layout, TextView is gonna be in the center. 现在,如果您恰好创建此布局,则TextView将位于中心。 This is the default behavior of the ConstraintLayout, it always tries to center views in it if you set constraints this way. 这是ConstraintLayout的默认行为,如果以这种方式设置约束,它将始终尝试在其中居中放置视图。 This behavior can be influenced by app:layout_constraintHorizontal_bias and app:layout_constraintVertical_bias property of the TextView. 此行为会受到TextView的app:layout_constraintHorizontal_biasapp:layout_constraintVertical_bias属性的影响。 Default values for both are 0.5 what you can translate to 50%. 两者的默认值为0.5,可以转换为50%。 If you set them both to 0, you will find the TextView in the top left position . 如果将它们都设置为0,则会在左上角找到TextView。 If you set them both to 1, the TextView will end up in the bottom right position . 如果将它们都设置为1,则TextView将最终位于右下位置 You got the idea, this way you can position your TextView or any View wherever you want. 您有了主意,这样就可以将TextView或任何View放置在所需的任何位置。

The bias properties are also described in the official documentation here . 偏差属性也在此处的官方文档中进行了描述。

Fully working example of TextView aligned to center vertically and aligned to right horizontally: 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>

使用设计,因为它比xml 视频文件容易得多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM