I have two TextView (tv1
and tv2
), and I want tv2 to be on the right of tv1, so I make a layout like this:
<LinearLayout 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"
android:orientation="horizontal">
<TextView
android:id="@ id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@ id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*" />
</LinearLayout>
If the text
of tv1 is within one line, everything is perfect. But if the text
is more than one line, tv1 will take all the space and squeeze tv2 out of the screen.
I think it may be because of Android lays out views in the order in which they appear in the layout file, so I change the root view from LinearLayour to ConstraintLayout and make tv2 first:
<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="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@ id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*"
app:layout_constraintLeft_toRightOf="@ id/tv1"
app:layout_constraintStart_toEndOf="@ id/tv1"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@ id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
This still doesn't work. I know I can use setLayoutParams
to set tv1
's width programmatically, but is there any other way besides programming?
CodePudding user response:
You can use wrap_content
for tv2 width and 0dp
for tv1 width:
<androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="@ id/tv1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/tv2"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@ id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@id/tv1"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
The tv2's width will minimum size for it setting wrap_content
while tv1 expands rest of the space between constraints (left and right).
CodePudding user response:
Try this its works for me. You have to set layout_width="0dp" and layout_weight="1" in both TextView.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@ id/tv1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20dp"
android:text="hello world ********** welcome"/>
<TextView
android:id="@ id/tv2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20dp"
android:text="hello world ********** welcome" />
</LinearLayout>