Long Android TextView 将其他视图推离屏幕

Long Android TextView pushes other views off-screen

我有两个并排的 TextView。 TextView1 的文本长度不一,而 TextView2 始终显示“+#”。但是,当 TextView1 变长时,它将 TextView2 推离屏幕。任何想法如何解决这一问题?这是我的布局代码:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/TextView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:ellipsize="end"
            android:textSize="13sp"/>

        <TextView
            android:id="@+id/TextView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textSize="13sp"/>

    </RelativeLayout>

在子视图上使用带有权重属性的 LinearLayout

<LinearLayout
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  xmlns:android="http://schemas.android.com/apk/res/android">

        <TextView
            android:id="@+id/TextView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:ellipsize="end"
            android:textSize="13sp"
            android:layout_weight="1"/>

        <TextView
            android:id="@+id/TextView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textSize="13sp"
            android:layout_weight="0"/>

    </LinearLayout>

这实际上是我一段时间以来一直试图解决的问题。不幸的是,其他人建议的方法 - 在 LinearLayout 中使用 layout_weight - 实际上不起作用;不过,我已经为您找到了解决方案!

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="left">

    <TextView
        android:id="@+id/TextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/TextView2"
        android:singleLine="true"
        android:ellipsize="end"
        android:textSize="13sp"/>

    <TextView
        android:id="@+id/TextView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:singleLine="true"
        android:textSize="13sp"/>
</RelativeLayout>

对于上面的块,我们使用 RelativeLayout 以便将第一个 TextView 对齐到第二个 TextView 的左侧。我们还将第二个 TextView 对齐到父 ViewGroup 的右侧。最后,我们将 android:gravity="left" 添加到父级 ViewGroup,以便将所有 TextView 都向左对齐。

这会导致两个 TextView 并排排列 - 无论第一个 TextView 的长度如何。如果您希望第一个 TextView 有多行,只需删除 android:ellipsize="end" 标签。

希望这是您的预期结果!

这个问题可以使用 android.support.constraint.ConstraintLayout

来解决

这里是屏幕截图,因此您可以看到它如何处理小而长的文本。

这里是XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    android:layout_margin="15dp">

    <ScrollView
        android:id="@+id/scrollView"
        android:background="@drawable/white_rounded_rectangle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:scrollbars="none">

        <TextView
            android:id="@+id/messageTv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif"
            android:lineSpacingExtra="6sp"
            android:paddingTop="15dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingBottom="70dp"
            android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever PageMaker including versions of Lorem Ipsum."
            android:textColor="#252525"
            android:textIsSelectable="true"
            android:textSize="@dimen/text_size16"
            />
    </ScrollView>


    <android.support.constraint.ConstraintLayout
        android:id="@+id/bottomLayout"
        android:layout_width="match_parent"
        android:layout_height="84dp"
        android:background="@drawable/white_rounded_rectangle"
        app:layout_constraintBottom_toBottomOf="@+id/scrollView"
        app:layout_constraintEnd_toEndOf="@+id/scrollView"
        app:layout_constraintStart_toStartOf="@+id/scrollView">

        <android.support.v7.widget.AppCompatButton
            android:id="@+id/btnAction1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_marginBottom="19dp"
            android:layout_weight="1"
            android:text="Action 1"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />

        <android.support.v7.widget.AppCompatButton
            android:id="@+id/btnAction2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="17dp"
            android:layout_marginBottom="16dp"
            android:text="Action 2"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/btnAction1" />
    </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>