I need to build UI with two TextViews which are constrained first top parent, and second is constrained below first TextView. The problem comes when text_one has more text and bring text_two outside the screen. The desired behavior is when text_one has more text text_two to be aligned bottom to parent and to don't go outside the screen. Something like weight 9:1 but this needs to be achieved only when text_one has more text. Normal behavior is represented in screen1 the problem is shown in screen2.
<?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="match_parent">
<TextView
android:id="@+id/txt_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#FF00"
android:text="the majority have suffered alteration in some form etc..."
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txt_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@color/colorPrimary"
android:text="It is a long established etc......"
app:layout_constraintTop_toBottomOf="@+id/txt_one"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
try the following sample it's maintain the other textView
height even if first one take larger space on the screen
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txt_one"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#FF00"
android:textSize="35sp"
android:text="the majority
e suffered alteration in some form etc..."
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintVertical_bias="0.0"
app:layout_constrainedHeight="true"
app:layout_constraintVertical_chainStyle="packed"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/txt_two"
/>
<TextView
android:id="@+id/txt_two"
android:layout_width="0dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="25sp"
android:background="@color/colorPrimary"
android:text="It is a long etc......"
app:layout_constraintTop_toBottomOf="@+id/txt_one"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
You can make use of
app:layout_constraintHeight_max
property of constraint layout.
For programmatically use
setMaxHeight()
In your use case, while screen rendering get screen height,
val text1MaxHeight = screenHeight*0.9
text1.setMaxHeight(text1MaxHeight)
It should work.
But If your textview1 will have more content, you would want it scrollable, so a new solution would suggest you to Encapsulate textview1 in scrollView, and take scrollview and textview2 in NestedScrollView like below
<NestedScrollView>
<ScrollView>
<TextView1/>
<ScrollView/>
<TextView2/>
<NestedScrollView>
Make nestedScrollingEnabled flag false in ScrollView, set ScrollView's maxHeight dynamically
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.