如果文本只有一行,如何將文本向右對齊,否則向左對齊(以便新行從左側開始,而不是從右側開始)?是否可以在 xml(非編程方式)中做到這一點?
我想要的是:


我有的:


代碼:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@ id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Label" />
<TextView
android:id="@ id/content"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/label"
app:layout_constraintTop_toTopOf="@id/label"
tools:text="Lorem ipsum" />
</androidx.constraintlayout.widget.ConstraintLayout>
uj5u.com熱心網友回復:
要實作此行為,您需要擁有屬性layout_constraintHorizontal_bias="1",layout_constrainedWidth="true"并 layout_width="wrap_content"進入內容 (@ id/content) TextView。
Xml 布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="wrap_content">
<TextView
android:id="@ id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:text="Label" />
<TextView
android:id="@ id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
app:layout_constrainedWidth="true"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@id/label"
app:layout_constraintEnd_toEndOf="parent"
tools:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." />
</androidx.constraintlayout.widget.ConstraintLayout>
小內容文字:

長內容文本:

uj5u.com熱心網友回復:
嘗試這個
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:layout_gravity="end"
android:text="Lorem ipsum" />
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/528319.html
標籤:安卓安卓布局安卓约束布局
