android - ConstraintLayout 在 TextView 中右对齐文本

标签 android textview android-constraintlayout

当 TextView 位于 ConstraintLayout 中时,如何对齐文本?我不能使用 layout_width="match_parent。试过这个,不起作用 :(

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

最佳答案

  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 使用不同的值.

关于android - ConstraintLayout 在 TextView 中右对齐文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50583351/

相关文章:

具有渐变*和*描边的Android TextView

android - TextVew 在提供协助信息时抛出异常

android - 在 ConstraintLayout 中使用 <include> 和 <merge>

android - ConstraintLayout,当约束依赖 View 消失时,布局 View 行为怪异

android - 如何将渐变定义为圆形progressBar

android - 无法在 android studio 中创建新 Activity

java - 通过 AttributeSet 访问 Widget 属性 - Android

java - GsonConverter 未解决

android - xml 属性 singleLine 在 Android 中是否已弃用?

android - 特定 api 级别的独家布局 - Android