使 2 个 TextViews 具有相同的高度

Make 2 TextViews have the same height

我有 2 个 TextView,一个在另一个之上。当他们有相同的文字时,他们有相同的高度,但当一个人有更多的文字时,他会变大。

<LinearLayout
                    android:orientation="vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_vertical"
                    android:layout_weight="1"
                    android:paddingLeft="8dp"
                    android:paddingTop="12dp">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:text="Math, Civics"
                        android:id="@+id/teaches"
                        android:layout_weight="1"
                        android:background="#A4D34A" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:text="Computer Science"
                        android:id="@+id/learns"
                        android:layout_weight="1"
                        android:background="#D22ACE" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:text="5$/hour"
                        android:id="@+id/rate" />

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="32dp"
                        android:layout_marginRight="8dp">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textAppearance="?android:attr/textAppearanceSmall"
                            android:text="Start Chat"
                            android:id="@+id/textView9"
                            android:layout_marginRight="8dp"
                            android:textStyle="italic"
                            android:layout_centerVertical="true"
                            android:layout_alignParentEnd="true" />
                    </RelativeLayout>

                </LinearLayout>

怎样才能让它们无论如何都保持在同一高度?

您有几个选择:您可以为两个文本视图提供相同的权重

<TextView
          android:layout_width="wrap_content"
          android:layout_height="0dp"
          android:layout_weight="1"/>
<TextView
          android:layout_width="wrap_content"
          android:layout_height="0dp"
          android:layout_weight="1"/>

或者您可以对两个视图使用固定高度。 或者您可以将文本视图的 MaxLines(最大行数)属性设置为特定值。

为你定义一个固定值 layout_height24dp 或 dimens.xml 中最好的东西:

<dimen name="textview_height">24dp</dimen>

并在您的布局中将 layout_height 都放在 :

@dimen/textview_height

另一种选择是将第一个选项设置为 wrap_content 并从它们中获取 MeasuredHeight ,然后取较大的值并将其应用于两个 TextViews