[英]Vertically align TextView text to TextInputLayout text in ConstraintLayout
[英]ConstraintLayout align text right in TextView
当文本在ConstraintLayout中时,如何在TextView中对齐文本? 我不能使用layout_width="match_parent
。尝试了一下,不起作用:(
android:gravity="start"
android:textAlignment="gravity"
使TextView与以下项成为父项:
android:layout_width="0dp"
然后定义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使用不同的值。
使用此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>
尝试这个:
<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" />
尝试这个:
<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" />
解:
使用此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>
希望能帮助到你。
通常,如果一行中只有一个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" />
不幸的是,我们丢失了布局的整个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_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>
使用设计,因为它比xml 视频文件容易得多
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.