为什么我的多行文本视图不具有相同的高度?
Why don't my multiline textviews have the same height?
我有 3 个文本视图,每行 2 行。我把它们放在水平 LinearLayout
中,重量相同。问题是,当我将文本放入 TextView
s 时,如果文本占用 1 行或 2 行,它们的高度就会不同。这种行为很奇怪。
无论文本长度如何,我都需要 3 个高度相同的文本视图。
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>
您需要设置 layout_height 一些高度而不是使用 wrap_content。
WRAP_CONTENT
:
View 请求的高度或宽度的特殊值。
WRAP_CONTENT会根据视图要求改变高度。
更新
你可以使用ConstraintLayout。这里所有的 TextView 高度都与 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>
我有 3 个文本视图,每行 2 行。我把它们放在水平 LinearLayout
中,重量相同。问题是,当我将文本放入 TextView
s 时,如果文本占用 1 行或 2 行,它们的高度就会不同。这种行为很奇怪。
无论文本长度如何,我都需要 3 个高度相同的文本视图。
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>
您需要设置 layout_height 一些高度而不是使用 wrap_content。
WRAP_CONTENT
:
View 请求的高度或宽度的特殊值。
WRAP_CONTENT会根据视图要求改变高度。
更新
你可以使用ConstraintLayout。这里所有的 TextView 高度都与 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>