'Why don't my multiline textviews have the same height?
I have 3 textviews with 2 lines. I placed them in the horizontal LinearLayout
with same weight. The problem is that when I put text into this TextView
s, they have different heights if text take up 1 line or 2 lines. This behavior is strange.
I need 3 text views with the same height regardless to the text length.
my_layout.xml
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="bottom">
<TextView
style="@style/style"
android:text="TextView1" />
<TextView
style="@style/style"
android:text="Long text Textview2" />
<TextView
style="@style/style"
android:text="TextView3" />
</LinearLayout>
styles.xml
:
<style name="style">
<item name="android:layout_width">0dp</item>
<item name="android:layout_weight">1</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">@color/black</item>
<item name="android:background">@color/white</item>
<item name="android:lines">2</item>
<item name="android:textSize">@dimen/txt_size_30</item>
</style>
Solution 1:[1]
You need to set layout_height with some height rather than using wrap_content.
WRAP_CONTENT
:
Special value for the height or width requested by a View.
WRAP_CONTENT will change the height according to view requirement.
Update
You can use ConstraintLayout. Here all the TextView height matches with height of long textView(TextView 2)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#dedede"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/textView2"
app:layout_constraintBottom_toBottomOf="parent"
android:text="textview 1"
app:layout_constraintTop_toTopOf="@id/textView2"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:background="#dedede"
android:layout_height="wrap_content"
app:layout_constraintStart_toEndOf="@id/textView1"
app:layout_constraintEnd_toStartOf="@id/textView3"
app:layout_constraintBottom_toBottomOf="parent"
android:text="long textView 2"
/>
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:background="#dedede"
android:layout_height="0dp"
app:layout_constraintStart_toEndOf="@id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:text="textView 3"
app:layout_constraintTop_toTopOf="@id/textView2"
/>
</android.support.constraint.ConstraintLayout>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |